Skip to content

Commit

Permalink
Merge pull request #93 from Kpoke/fix/seeds
Browse files Browse the repository at this point in the history
add seeds
  • Loading branch information
Kpoke authored Jul 20, 2022
2 parents e296450 + 3da2309 commit 6b3a01f
Show file tree
Hide file tree
Showing 9 changed files with 187 additions and 21 deletions.
2 changes: 1 addition & 1 deletion api-tests/seed.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ require('dotenv').config();
const { expect } = require('chai');
const log = require('loglevel');
const knex = require('../server/infra/database/knex');
const seed = require('../database/seed/index');
const seed = require('../database/single-seed');

describe('seed', () => {
beforeEach(async () => {
Expand Down
42 changes: 24 additions & 18 deletions database/seed-cli.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const log = require("loglevel");
const log = require('loglevel');
const { Command } = require('commander');
const seed = require('./seed/index');
const seed = require('./single-seed');

const program = new Command();

Expand All @@ -9,32 +9,38 @@ program
.description('CLI to seed data into DB for testing')
.version('0.1.0');

program.command('earnings')
program
.command('earnings')
.description('Seed an earings record into earning db')
// .argument('<string>', 'string to split')
.requiredOption('-f, --funder_id <string>', 'set up the funder id')
.requiredOption('-g, --grower_id <string>', 'set up the grower id')
.requiredOption('-c, --captures_count <string>', 'the number of captures')
.requiredOption('-s, --sub_organization <string>', 'the id of sub organization')
.requiredOption(
'-s, --sub_organization <string>',
'the id of sub organization',
)
.action((options) => {
// const limit = options.first ? 1 : undefined;
// console.log(str.split(options.separator, limit));
log.warn("seeding...", options);
seed.seed(
options.funder_id,
options.grower_id,
options.captures_count,
options.sub_organization,
)
log.warn('seeding...', options);
seed
.seed(
options.funder_id,
options.grower_id,
options.captures_count,
options.sub_organization,
)
.then(() => {
log.warn("done!");
log.warn('done!');
process.exit(0);
}).catch(e => {
log.error("error", e);
log.warn("seed failed!");
process.exit(1);
})
log.warn("executed...");
.catch((e) => {
log.error('error', e);
log.warn('seed failed!');
process.exit(1);
});
log.warn('executed...');
});

program.parse();
program.parse();
5 changes: 5 additions & 0 deletions database/seeds/00_job_database_cleaner.js
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;
`);
};
67 changes: 67 additions & 0 deletions database/seeds/01_earnings.js
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,
});
}
};
13 changes: 13 additions & 0 deletions database/seeds/data/funders.js
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;
34 changes: 34 additions & 0 deletions database/seeds/data/growers.js
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;
2 changes: 1 addition & 1 deletion database/seed/index.js → database/single-seed.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const log = require('loglevel');
const { v4: uuid } = require('uuid');
const knex = require('../../server/infra/database/knex');
const knex = require('../server/infra/database/knex');

exports.seed = async (
funder_id,
Expand Down
39 changes: 39 additions & 0 deletions knexfile.js
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',
},
};
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
"db-migrate-ci": "cd database; db-migrate up",
"prepare": "husky install",
"db-migrate-dev": "cd database; echo \"Migrating dev\"; db-migrate --env dev up; echo \"Migrating integration\"; db-migrate --env integration up",
"seed-cli": "node ./database/seed-cli.js"
"seed-cli": "node ./database/seed-cli.js",
"seed-dev": "NODE_ENV=development knex seed:run",
"seed-test": "NODE_ENV=test knex seed:run"
},
"keywords": [
"ecology"
Expand Down

0 comments on commit 6b3a01f

Please sign in to comment.