generated from Greenstand/treetracker-microservice-template
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #93 from Kpoke/fix/seeds
add seeds
- Loading branch information
Showing
9 changed files
with
187 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
exports.seed = async function (knex) { | ||
await knex.raw(` | ||
DELETE FROM earnings; | ||
`); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
const { v4: uuid } = require('uuid'); | ||
const growers = require('./data/growers'); | ||
const funders = require('./data/funders'); | ||
|
||
const randomIntFromInterval = (min, max) => { | ||
return Math.floor(Math.random() * (max - min + 1) + min); | ||
}; | ||
const maxPayout = 12000; | ||
|
||
exports.seed = async function (knex) { | ||
for (let i = 0; i < 10; i += 1) { | ||
const grower_id = growers.pop().id; | ||
|
||
const captures_count1 = randomIntFromInterval(400, 1100); | ||
|
||
let multiplier1 = (captures_count1 - (captures_count1 % 100)) / 10 / 100; | ||
if (multiplier1 > 1) { | ||
multiplier1 = 1; | ||
} | ||
const amount1 = multiplier1 * maxPayout; | ||
|
||
const captures_count2 = randomIntFromInterval(400, 1100); | ||
|
||
let multiplier2 = (captures_count2 - (captures_count2 % 100)) / 10 / 100; | ||
if (multiplier2 > 1) { | ||
multiplier2 = 1; | ||
} | ||
const amount2 = multiplier2 * maxPayout; | ||
|
||
const funder_id = funders[randomIntFromInterval(0, 2)].id; | ||
const consolidation_rule_id = uuid(); | ||
|
||
await knex('earnings').insert({ | ||
worker_id: grower_id, | ||
amount: amount1, | ||
payment_confirmation_id: null, | ||
payment_method: null, | ||
currency: 'USD', | ||
status: 'calculated', | ||
paid_at: null, | ||
contract_id: '483a1f4e-0c52-4b53-b917-5ff4311ded26', | ||
funder_id, | ||
calculated_at: '02/24/2022', | ||
consolidation_rule_id, | ||
consolidation_period_start: '01/25/2022', | ||
consolidation_period_end: '02/25/2022', | ||
captures_count: captures_count1, | ||
}); | ||
|
||
await knex('earnings').insert({ | ||
worker_id: grower_id, | ||
amount: amount2, | ||
payment_confirmation_id: null, | ||
payment_method: null, | ||
currency: 'USD', | ||
status: 'calculated', | ||
paid_at: null, | ||
contract_id: '483a1f4e-0c52-4b53-b917-5ff4311ded26', | ||
funder_id, | ||
calculated_at: '02/24/2022', | ||
consolidation_rule_id, | ||
consolidation_period_start: '02/25/2022', | ||
consolidation_period_end: '03/25/2022', | ||
captures_count: captures_count2, | ||
}); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
const funders = [ | ||
{ | ||
id: '792a4eee-8e18-4750-a56f-91bdec383aa6', | ||
}, | ||
{ | ||
id: 'b683313a-f082-4829-9127-59a176be916d', | ||
}, | ||
{ | ||
id: 'c92189d2-2d55-44bc-a0b4-0dad25dc9f35', | ||
}, | ||
]; | ||
|
||
module.exports = funders; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
const growers = [ | ||
{ | ||
id: '35a23de8-f1ab-4409-be79-3c6a158d5bde', | ||
}, | ||
{ | ||
id: '90eef52b-ad55-4953-ace9-6a324ae6cec2', | ||
}, | ||
{ | ||
id: '9ae7337d-f3eb-4151-935e-51138ccdc811', | ||
}, | ||
{ | ||
id: 'da01db0e-8686-4aaa-833f-eaf8aa85f664', | ||
}, | ||
{ | ||
id: '03f8502b-01f9-44a8-b072-1c4638d867c5', | ||
}, | ||
{ | ||
id: 'c3d17aa9-b45d-4e62-939d-bf33cf3ef3ed', | ||
}, | ||
{ | ||
id: 'a3896425-4263-4f3b-af48-44f7ac07b2f2', | ||
}, | ||
{ | ||
id: 'b9ce5741-38f1-4616-9792-dbb9990b0d20', | ||
}, | ||
{ | ||
id: 'd7429ba9-7e64-4e84-a774-5b8ea7b4f433', | ||
}, | ||
{ | ||
id: 'ed538c0f-73d0-49eb-8410-25efe94b421b', | ||
}, | ||
]; | ||
|
||
module.exports = growers; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
require('dotenv').config({ path: `.env.${process.env.NODE_ENV}` }); | ||
const path = require('path'); | ||
|
||
const connection = process.env.DATABASE_URL_SEEDER; | ||
|
||
const postgresPattern = /^postgresql:\//; | ||
|
||
if (!postgresPattern.test(connection)) { | ||
throw new Error('invalid database connection url received'); | ||
} | ||
|
||
module.exports = { | ||
development: { | ||
client: 'pg', | ||
connection, | ||
searchPath: [process.env.DATABASE_SCHEMA, 'public'], | ||
pool: { | ||
min: 1, | ||
max: 10, | ||
}, | ||
seeds: { | ||
directory: path.join(__dirname, 'database', 'seeds'), | ||
}, | ||
debug: process.env.NODE_LOG_LEVEL === 'debug', | ||
}, | ||
test: { | ||
client: 'pg', | ||
connection, | ||
searchPath: [process.env.DATABASE_SCHEMA, 'public'], | ||
pool: { | ||
min: 1, | ||
max: 10, | ||
}, | ||
seeds: { | ||
directory: path.join(__dirname, 'database', 'seeds'), | ||
}, | ||
debug: process.env.NODE_LOG_LEVEL === 'debug', | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters