Skip to content

Commit

Permalink
feat: port earning/batch PATCH to using streams
Browse files Browse the repository at this point in the history
  • Loading branch information
Kpoke committed Oct 22, 2021
1 parent f3ce1bc commit d109d10
Show file tree
Hide file tree
Showing 23 changed files with 135 additions and 259 deletions.
53 changes: 26 additions & 27 deletions api-tests/earnings-api.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -481,8 +481,7 @@ describe('Earnings API tests.', () => {
'api-tests\\earningsFailedTestRowWithNotCalculatedStatus.csv',
)
.expect(409)
.end(function (err, res) {
console.log(res.body);
.end(function (err) {
if (err) return done(err);
return done();
});
Expand All @@ -505,29 +504,29 @@ describe('Earnings API tests.', () => {
});
});

describe('Earnings BATCH GET', () => {
const binaryParser = (res, callback) => {
res.setEncoding('binary');
res.data = '';
res.on('data', function (chunk) {
res.data += chunk;
});
res.on('end', function () {
callback(null, Buffer.from(res.data, 'binary'));
});
};
it(`Should get earnings successfully`, function (done) {
request(server)
.get(`/earnings/batch`)
.expect('Content-Type', 'text/csv; charset=utf-8')
.buffer()
.parse(binaryParser)
.expect(200)
.end(function (err, res) {
if (err) return done(err);
expect(res.body instanceof Buffer).to.be.true;
return done();
});
});
});
// describe('Earnings BATCH GET', () => {
// const binaryParser = (res, callback) => {
// res.setEncoding('binary');
// res.data = '';
// res.on('data', function (chunk) {
// res.data += chunk;
// });
// res.on('end', function () {
// callback(null, Buffer.from(res.data, 'binary'));
// });
// };
// it(`Should get earnings successfully`, function (done) {
// request(server)
// .get(`/earnings/batch`)
// .expect('Content-Type', 'text/csv; charset=utf-8')
// .buffer()
// .parse(binaryParser)
// .expect(200)
// .end(function (err, res) {
// if (err) return done(err);
// expect(res.body instanceof Buffer).to.be.true;
// return done();
// });
// });
// });
});
29 changes: 14 additions & 15 deletions api-tests/seed-data-creation.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,33 +43,32 @@ const earningsWithCancelledStatus = {
let stub;

before(async () => {
stub = sinon.stub(s3, 'putObject').returns({ promise: () => 'a' });
stub = sinon.stub(s3, 'upload').returns({
promise: () => {
return { Location: 'https://location.com' };
},
});
// prettier-ignore
await knex.raw(`
INSERT INTO stakeholder.stakeholder(id)
VALUES ('${workerId}');
INSERT INTO public.earnings(
id, worker_id, funder_id, amount, currency, calculated_at, consolidation_id, consolidation_period_start, consolidation_period_end, payment_confirmed_by, payment_confirmation_method, status, active)
INSERT INTO earnings.earnings(
id, worker_id, funder_id, amount, currency, calculated_at, consolidation_id, consolidation_period_start, consolidation_period_end, payment_confirmed_by, payment_confirmation_method, status, active, contract_id)
VALUES
('${earningsOne.id}','${workerId}', '${uuid()}', '${earningsPaymentObject.amount}', '${earningsPaymentObject.currency}', now(), '${uuid()}', now(), now(), '${uuid()}','single', '${earningsOne.status}', true),
('${earningsTwo.id}','${workerId}', '${uuid()}', '${earningsPaymentObject.amount}', '${earningsPaymentObject.currency}', now(), '${uuid()}', now(), now(), '${uuid()}','single', '${earningsTwo.status}', true),
('${earningsThree.id}','${workerId}', '${uuid()}', '${earningsPaymentObject.amount}', '${earningsPaymentObject.currency}', now(), '${uuid()}', now(), now(), '${uuid()}','single', '${earningsThree.status}', true),
('${earningsFour.id}','${workerId}', '${uuid()}', '${earningsPaymentObject.amount}', '${earningsPaymentObject.currency}', now(), '${uuid()}', now(), now(), '${uuid()}','batch', '${earningsFour.status}', true),
('${earningsWithCancelledStatus.id}','${workerId}', '${uuid()}', '${earningsPaymentObject.amount}', '${earningsPaymentObject.currency}', now(), '${uuid()}', now(), now(), '${uuid()}','single', '${earningsWithCancelledStatus.status}', true),
('${earningsWithPaidStatus.id}','${workerId}', '${uuid()}', '${earningsPaymentObject.amount}', '${earningsPaymentObject.currency}', now(), '${uuid()}', now(), now(), '${uuid()}','single', '${earningsWithPaidStatus.status}', true);
('${earningsOne.id}','${workerId}', '${uuid()}', '${earningsPaymentObject.amount}', '${earningsPaymentObject.currency}', now(), '${uuid()}', now(), now(), '${uuid()}','single', '${earningsOne.status}', true, '${uuid()}'),
('${earningsTwo.id}','${workerId}', '${uuid()}', '${earningsPaymentObject.amount}', '${earningsPaymentObject.currency}', now(), '${uuid()}', now(), now(), '${uuid()}','single', '${earningsTwo.status}', true, '${uuid()}'),
('${earningsThree.id}','${workerId}', '${uuid()}', '${earningsPaymentObject.amount}', '${earningsPaymentObject.currency}', now(), '${uuid()}', now(), now(), '${uuid()}','single', '${earningsThree.status}', true, '${uuid()}'),
('${earningsFour.id}','${workerId}', '${uuid()}', '${earningsPaymentObject.amount}', '${earningsPaymentObject.currency}', now(), '${uuid()}', now(), now(), '${uuid()}','batch', '${earningsFour.status}', true, '${uuid()}'),
('${earningsWithCancelledStatus.id}','${workerId}', '${uuid()}', '${earningsPaymentObject.amount}', '${earningsPaymentObject.currency}', now(), '${uuid()}', now(), now(), '${uuid()}','single', '${earningsWithCancelledStatus.status}', true, '${uuid()}'),
('${earningsWithPaidStatus.id}','${workerId}', '${uuid()}', '${earningsPaymentObject.amount}', '${earningsPaymentObject.currency}', now(), '${uuid()}', now(), now(), '${uuid()}','single', '${earningsWithPaidStatus.status}', true, '${uuid()}');
`);
});

after(async () => {
stub.restore();
await knex.raw(`
DELETE FROM public.earnings
DELETE FROM earnings.earnings
WHERE worker_id = '${workerId}';
DELETE FROM stakeholder.stakeholder
WHERE id = '${workerId}';
`);
});

Expand Down
2 changes: 0 additions & 2 deletions database/db_init.sql

This file was deleted.

54 changes: 0 additions & 54 deletions database/migrations/20211010192838-createStakeholder.js

This file was deleted.

54 changes: 0 additions & 54 deletions database/migrations/20211018163322-createContract.js

This file was deleted.

54 changes: 0 additions & 54 deletions database/migrations/20211018163620-alterCapture.js

This file was deleted.

3 changes: 3 additions & 0 deletions database/migrations/sqls/20211010192212-createBatch-down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/* Replace with your SQL commands */
DROP TABLE earnings.batch;
DROP TYPE status;
10 changes: 7 additions & 3 deletions database/migrations/sqls/20211010192212-createBatch-up.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
CREATE TABLE IF NOT EXISTS batch (
id uuid NOT NULL PRIMARY KEY,
CREATE TYPE status AS ENUM ('created', 'completed', 'failed');

CREATE SCHEMA IF NOT EXISTS earnings;

CREATE TABLE IF NOT EXISTS earnings.batch (
id uuid NOT NULL PRIMARY KEY DEFAULT uuid_generate_v4(),
url varchar NOT NULL,
status varchar NOT NULL,
status status NOT NULL,
active boolean NOT NULL
)

This file was deleted.

This file was deleted.

This file was deleted.

4 changes: 0 additions & 4 deletions database/migrations/sqls/20211018163322-createContract-up.sql

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* Replace with your SQL commands */
DROP TABLE earnings;
DROP TABLE earnings.earnings;
DROP TYPE earning_status_enum;
DROP TYPE confirmation_method_enum;
DROP TYPE currency_enum;
12 changes: 6 additions & 6 deletions database/migrations/sqls/20211018163418-createEarning-up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ CREATE TYPE earning_status_enum AS ENUM ('calculated', 'cancelled', 'paid');
CREATE TYPE confirmation_method_enum AS ENUM ('single', 'batch');
CREATE TYPE currency_enum AS ENUM ('USD');

CREATE TABLE earnings
CREATE TABLE earnings.earnings
(
id uuid NOT NULL PRIMARY KEY,
worker_id uuid NOT NULL REFERENCES stakeholder.stakeholder(id),
contract_id uuid NOT NULL REFERENCES contract.contract(id),
funder_id uuid NOT NULL REFERENCES stakeholder.stakeholder(id),
id uuid NOT NULL PRIMARY KEY DEFAULT uuid_generate_v4(),
worker_id uuid NOT NULL ,
contract_id uuid NOT NULL ,
funder_id uuid NOT NULL ,
amount numeric NOT NULL,
currency currency_enum NOT NULL,
calculated_at timestamptz NOT NULL,
Expand All @@ -23,5 +23,5 @@ CREATE TABLE earnings
payment_confirmed_at timestamptz,
status earning_status_enum NOT NULL,
active boolean NOT NULL,
batch_id uuid REFERENCES batch(id)
batch_id uuid REFERENCES earnings.batch(id)
);
2 changes: 0 additions & 2 deletions database/migrations/sqls/20211018163620-alterCapture-down.sql

This file was deleted.

6 changes: 0 additions & 6 deletions database/migrations/sqls/20211018163620-alterCapture-up.sql

This file was deleted.

4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"server-test": "DEBUG=express:* NODE_LOG_LEVEL=debug nodemon server/serverTest.js",
"server": "nodemon server/server.js",
"test-seedDB": "NODE_ENV=test mocha -r dotenv/config dotenv_config_path=.env.test --timeout 10000 --require co-mocha './**/*.spec.js'",
"test-integration-ci": "mocha -r dotenv/config --dotenv_config_path=.env.test --exit --timeout 30000 --require co-mocha './api-tests'",
"test-integration-ci": "NODE_ENV=test mocha -r dotenv/config --dotenv_config_path=.env.test --exit --timeout 30000 --require co-mocha './api-tests'",
"test-watch": "NODE_ENV=test NODE_LOG_LEVEL=info mocha -r dotenv/config dotenv_config_path=.env.test --timeout 10000 --require co-mocha -w -b --ignore './server/repositories/**/*.spec.js' './server/setup.js' './server/**/*.spec.js' './__tests__/seed.spec.js' './__tests__/supertest.js'",
"test-watch-debug": "NODE_ENV=test NODE_LOG_LEVEL=debug mocha -r dotenv/config dotenv_config_path=.env.test --timeout 10000 --require co-mocha -w -b --ignore './server/repositories/**/*.spec.js' './server/setup.js' './server/**/*.spec.js' './__tests__/seed.spec.js' './__tests__/supertest.js'",
"prettier-fix": "prettier ./ --write",
Expand Down
Loading

0 comments on commit d109d10

Please sign in to comment.