From 48e7e170c87850a99b76228e03e79dcce7d5d69a Mon Sep 17 00:00:00 2001 From: deanchen Date: Wed, 16 Mar 2022 05:29:33 +0800 Subject: [PATCH] feat: add capture_count --- __tests__/api-tests/earnings-api.spec.js | 2 +- .../api-tests/earningsFailedTestInvalidHeader.csv | 8 ++++---- .../earningsFailedTestInvalidHeader2.csv | 8 ++++---- .../earningsFailedTestInvalidHeader3.csv | 8 ++++---- .../earningsFailedTestInvalidHeader5.csv | 8 ++++---- .../earningsFailedTestInvalidHeader6.csv | 8 ++++---- .../api-tests/earningsFailedTestInvalidRow.csv | 8 ++++---- ...rningsFailedTestRowWithNotCalculatedStatus.csv | 12 ++++++------ __tests__/api-tests/earningsSuccessfulTest.csv | 8 ++++---- __tests__/api-tests/seed-data-creation.js | 15 ++++++++------- __tests__/api-tests/seed.spec.js | 2 +- server/handlers/earningsHandler.js | 1 + server/models/Earnings.js | 11 ++++++++++- 13 files changed, 55 insertions(+), 44 deletions(-) diff --git a/__tests__/api-tests/earnings-api.spec.js b/__tests__/api-tests/earnings-api.spec.js index 921a377..f50e8f4 100644 --- a/__tests__/api-tests/earnings-api.spec.js +++ b/__tests__/api-tests/earnings-api.spec.js @@ -473,7 +473,6 @@ describe('Earnings API tests.', () => { expect(earning).to.have.keys([ 'id', 'grower', - 'funder', 'captures_count', 'phone', 'worker_id', @@ -653,6 +652,7 @@ describe('Earnings API tests.', () => { }); it(`Successful batch request`, function (done) { + request(server) .patch(`/earnings/batch`) .set('Accept', 'multipart/form-data') diff --git a/__tests__/api-tests/earningsFailedTestInvalidHeader.csv b/__tests__/api-tests/earningsFailedTestInvalidHeader.csv index 2ce05e1..86f313a 100644 --- a/__tests__/api-tests/earningsFailedTestInvalidHeader.csv +++ b/__tests__/api-tests/earningsFailedTestInvalidHeader.csv @@ -1,4 +1,4 @@ -earnings_id,worker_id,phone,currency,amount,payment_confirmation_id,payment_id -f3056760-4c36-4cf7-b625-ed656d314794,71be6266-81fe-476f-a563-9bc1c61fc037,Not sure for now,USD,700,2,jomzy jor jor,2021-10-10T00:00:00.000Z -a69e5cec-945f-4693-8a94-01623ee44187,71be6266-81fe-476f-a563-9bc1c61fc037,Not sure for now,USD,700,3,jomzy jor jor,2021-10-10T00:00:00.000Z -61714d24-6131-4296-ae36-30a7199cc645,71be6266-81fe-476f-a563-9bc1c61fc037,Not sure for now,USD,700,4,jomzy jor jor,2021-10-10T00:00:00.000Z \ No newline at end of file +earnings_id,worker_id,phone,currency,amount,captures_count,payment_confirmation_id,payment_id +f3056760-4c36-4cf7-b625-ed656d314794,71be6266-81fe-476f-a563-9bc1c61fc037,Not sure for now,USD,700,22,2,jomzy jor jor,2021-10-10T00:00:00.000Z +a69e5cec-945f-4693-8a94-01623ee44187,71be6266-81fe-476f-a563-9bc1c61fc037,Not sure for now,USD,700,22,3,jomzy jor jor,2021-10-10T00:00:00.000Z +61714d24-6131-4296-ae36-30a7199cc645,71be6266-81fe-476f-a563-9bc1c61fc037,Not sure for now,USD,700,22,4,jomzy jor jor,2021-10-10T00:00:00.000Z \ No newline at end of file diff --git a/__tests__/api-tests/earningsFailedTestInvalidHeader2.csv b/__tests__/api-tests/earningsFailedTestInvalidHeader2.csv index e1661c3..e6eb5b8 100644 --- a/__tests__/api-tests/earningsFailedTestInvalidHeader2.csv +++ b/__tests__/api-tests/earningsFailedTestInvalidHeader2.csv @@ -1,4 +1,4 @@ -earnings_id,worker_id,phone,currency,amount,payment_id,payment_method -f3056760-4c36-4cf7-b625-ed656d314794,71be6266-81fe-476f-a563-9bc1c61fc037,Not sure for now,USD,700,2,jomzy jor jor,2021-10-10T00:00:00.000Z -a69e5cec-945f-4693-8a94-01623ee44187,71be6266-81fe-476f-a563-9bc1c61fc037,Not sure for now,USD,700,3,jomzy jor jor,2021-10-10T00:00:00.000Z -61714d24-6131-4296-ae36-30a7199cc645,71be6266-81fe-476f-a563-9bc1c61fc037,Not sure for now,USD,700,4,jomzy jor jor,2021-10-10T00:00:00.000Z \ No newline at end of file +earnings_id,worker_id,phone,currency,amount,captures_count,payment_id,payment_method +f3056760-4c36-4cf7-b625-ed656d314794,71be6266-81fe-476f-a563-9bc1c61fc037,Not sure for now,USD,700,22,2,jomzy jor jor,2021-10-10T00:00:00.000Z +a69e5cec-945f-4693-8a94-01623ee44187,71be6266-81fe-476f-a563-9bc1c61fc037,Not sure for now,USD,700,22,3,jomzy jor jor,2021-10-10T00:00:00.000Z +61714d24-6131-4296-ae36-30a7199cc645,71be6266-81fe-476f-a563-9bc1c61fc037,Not sure for now,USD,700,22,4,jomzy jor jor,2021-10-10T00:00:00.000Z \ No newline at end of file diff --git a/__tests__/api-tests/earningsFailedTestInvalidHeader3.csv b/__tests__/api-tests/earningsFailedTestInvalidHeader3.csv index 80508db..342d8ac 100644 --- a/__tests__/api-tests/earningsFailedTestInvalidHeader3.csv +++ b/__tests__/api-tests/earningsFailedTestInvalidHeader3.csv @@ -1,4 +1,4 @@ -earnings_id,phone,currency,amount,payment_confirmation_id,payment_method -f3056760-4c36-4cf7-b625-ed656d314794,71be6266-81fe-476f-a563-9bc1c61fc037,Not sure for now,USD,700,2,jomzy jor jor,2021-10-10T00:00:00.000Z -a69e5cec-945f-4693-8a94-01623ee44187,71be6266-81fe-476f-a563-9bc1c61fc037,Not sure for now,USD,700,3,jomzy jor jor,2021-10-10T00:00:00.000Z -61714d24-6131-4296-ae36-30a7199cc645,71be6266-81fe-476f-a563-9bc1c61fc037,Not sure for now,USD,700,4,jomzy jor jor,2021-10-10T00:00:00.000Z \ No newline at end of file +earnings_id,phone,currency,amount,captures_count,payment_confirmation_id,payment_method +f3056760-4c36-4cf7-b625-ed656d314794,71be6266-81fe-476f-a563-9bc1c61fc037,Not sure for now,USD,700,22,2,jomzy jor jor,2021-10-10T00:00:00.000Z +a69e5cec-945f-4693-8a94-01623ee44187,71be6266-81fe-476f-a563-9bc1c61fc037,Not sure for now,USD,700,22,3,jomzy jor jor,2021-10-10T00:00:00.000Z +61714d24-6131-4296-ae36-30a7199cc645,71be6266-81fe-476f-a563-9bc1c61fc037,Not sure for now,USD,700,22,4,jomzy jor jor,2021-10-10T00:00:00.000Z \ No newline at end of file diff --git a/__tests__/api-tests/earningsFailedTestInvalidHeader5.csv b/__tests__/api-tests/earningsFailedTestInvalidHeader5.csv index afc382e..8683af2 100644 --- a/__tests__/api-tests/earningsFailedTestInvalidHeader5.csv +++ b/__tests__/api-tests/earningsFailedTestInvalidHeader5.csv @@ -1,4 +1,4 @@ -earnings_id,worker_id,phone,amount,payment_confirmation_id,payment_method -f3056760-4c36-4cf7-b625-ed656d314794,71be6266-81fe-476f-a563-9bc1c61fc037,Not sure for now,USD,700,2,jomzy jor jor,2021-10-10T00:00:00.000Z -a69e5cec-945f-4693-8a94-01623ee44187,71be6266-81fe-476f-a563-9bc1c61fc037,Not sure for now,USD,700,3,jomzy jor jor,2021-10-10T00:00:00.000Z -61714d24-6131-4296-ae36-30a7199cc645,71be6266-81fe-476f-a563-9bc1c61fc037,Not sure for now,USD,700,4,jomzy jor jor,2021-10-10T00:00:00.000Z \ No newline at end of file +earnings_id,worker_id,phone,amount,captures_count,payment_confirmation_id,payment_method +f3056760-4c36-4cf7-b625-ed656d314794,71be6266-81fe-476f-a563-9bc1c61fc037,Not sure for now,USD,700,22,2,jomzy jor jor,2021-10-10T00:00:00.000Z +a69e5cec-945f-4693-8a94-01623ee44187,71be6266-81fe-476f-a563-9bc1c61fc037,Not sure for now,USD,700,22,3,jomzy jor jor,2021-10-10T00:00:00.000Z +61714d24-6131-4296-ae36-30a7199cc645,71be6266-81fe-476f-a563-9bc1c61fc037,Not sure for now,USD,700,22,4,jomzy jor jor,2021-10-10T00:00:00.000Z \ No newline at end of file diff --git a/__tests__/api-tests/earningsFailedTestInvalidHeader6.csv b/__tests__/api-tests/earningsFailedTestInvalidHeader6.csv index d4dfc02..73f4fd5 100644 --- a/__tests__/api-tests/earningsFailedTestInvalidHeader6.csv +++ b/__tests__/api-tests/earningsFailedTestInvalidHeader6.csv @@ -1,4 +1,4 @@ -worker_id,phone,currency,amount,payment_confirmation_id,payment_method -f3056760-4c36-4cf7-b625-ed656d314794,71be6266-81fe-476f-a563-9bc1c61fc037,Not sure for now,USD,700,2,jomzy jor jor,2021-10-10T00:00:00.000Z -a69e5cec-945f-4693-8a94-01623ee44187,71be6266-81fe-476f-a563-9bc1c61fc037,Not sure for now,USD,700,3,jomzy jor jor,2021-10-10T00:00:00.000Z -61714d24-6131-4296-ae36-30a7199cc645,71be6266-81fe-476f-a563-9bc1c61fc037,Not sure for now,USD,700,4,jomzy jor jor,2021-10-10T00:00:00.000Z \ No newline at end of file +worker_id,phone,currency,amount,captures_count,payment_confirmation_id,payment_method +f3056760-4c36-4cf7-b625-ed656d314794,71be6266-81fe-476f-a563-9bc1c61fc037,Not sure for now,USD,700,22,2,jomzy jor jor,2021-10-10T00:00:00.000Z +a69e5cec-945f-4693-8a94-01623ee44187,71be6266-81fe-476f-a563-9bc1c61fc037,Not sure for now,USD,700,22,3,jomzy jor jor,2021-10-10T00:00:00.000Z +61714d24-6131-4296-ae36-30a7199cc645,71be6266-81fe-476f-a563-9bc1c61fc037,Not sure for now,USD,700,22,4,jomzy jor jor,2021-10-10T00:00:00.000Z \ No newline at end of file diff --git a/__tests__/api-tests/earningsFailedTestInvalidRow.csv b/__tests__/api-tests/earningsFailedTestInvalidRow.csv index b687005..8061123 100644 --- a/__tests__/api-tests/earningsFailedTestInvalidRow.csv +++ b/__tests__/api-tests/earningsFailedTestInvalidRow.csv @@ -1,4 +1,4 @@ -earnings_id,worker_id,phone,currency,amount,payment_confirmation_id,payment_method,paid_at -f3056760-4c36-4cf7-b625-ed6,71be6266-81fe-476f-a563-9bc1c61fc037,Not sure for now,USD,700,2,jomzy jor jor,2021-10-10T00:00:00.000Z -a69e5cec-945f-4693-8a94-01623ee44187,71be6266-81fe-476f-a563-9bc1c61fc037,Not sure for now,USD,700,3,jomzy jor jor,2021-10-10T00:00:00.000Z -61714d24-6131-4296-ae36-30a7199cc645,Not sure for now,USD,700,4,jomzy jor jor,2021-10-10T00:00:00.000Z \ No newline at end of file +earnings_id,worker_id,phone,currency,amount,captures_count,payment_confirmation_id,payment_method,paid_at +f3056760-4c36-4cf7-b625-ed6,71be6266-81fe-476f-a563-9bc1c61fc037,Not sure for now,USD,700,22,2,jomzy jor jor,2021-10-10T00:00:00.000Z +a69e5cec-945f-4693-8a94-01623ee44187,71be6266-81fe-476f-a563-9bc1c61fc037,Not sure for now,USD,700,22,3,jomzy jor jor,2021-10-10T00:00:00.000Z +61714d24-6131-4296-ae36-30a7199cc645,Not sure for now,USD,700,22,4,jomzy jor jor,2021-10-10T00:00:00.000Z \ No newline at end of file diff --git a/__tests__/api-tests/earningsFailedTestRowWithNotCalculatedStatus.csv b/__tests__/api-tests/earningsFailedTestRowWithNotCalculatedStatus.csv index 91f6c2e..45b591c 100644 --- a/__tests__/api-tests/earningsFailedTestRowWithNotCalculatedStatus.csv +++ b/__tests__/api-tests/earningsFailedTestRowWithNotCalculatedStatus.csv @@ -1,6 +1,6 @@ -earnings_id,worker_id,phone,currency,amount,payment_confirmation_id,payment_method,paid_at -f3056760-4c36-4cf7-b625-ed656d314794,71be6266-81fe-476f-a563-9bc1c61fc037,Not sure for now,USD,700,2,jomzy jor jor,2021-10-10T00:00:00.000Z -a69e5cec-945f-4693-8a94-01623ee44187,71be6266-81fe-476f-a563-9bc1c61fc037,Not sure for now,USD,700,3,jomzy jor jor,2021-10-10T00:00:00.000Z -61714d24-6131-4296-ae36-30a7199cc645,71be6266-81fe-476f-a563-9bc1c61fc037,Not sure for now,USD,700,4,jomzy jor jor,2021-10-10T00:00:00.000Z -9c10e443-4e08-40d4-9b73-5a931886f896,71be6266-81fe-476f-a563-9bc1c61fc037,Not sure for now,USD,700,4,jomzy jor jor,2021-10-10T00:00:00.000Z -3ab96dfd-274a-4097-8e7d-942e58203784,71be6266-81fe-476f-a563-9bc1c61fc037,Not sure for now,USD,700,4,jomzy jor jor,2021-10-10T00:00:00.000Z \ No newline at end of file +earnings_id,worker_id,phone,currency,amount,captures_count,payment_confirmation_id,payment_method,paid_at +f3056760-4c36-4cf7-b625-ed656d314794,71be6266-81fe-476f-a563-9bc1c61fc037,Not sure for now,USD,700,22,2,jomzy jor jor,2021-10-10T00:00:00.000Z +a69e5cec-945f-4693-8a94-01623ee44187,71be6266-81fe-476f-a563-9bc1c61fc037,Not sure for now,USD,700,22,3,jomzy jor jor,2021-10-10T00:00:00.000Z +61714d24-6131-4296-ae36-30a7199cc645,71be6266-81fe-476f-a563-9bc1c61fc037,Not sure for now,USD,700,22,4,jomzy jor jor,2021-10-10T00:00:00.000Z +9c10e443-4e08-40d4-9b73-5a931886f896,71be6266-81fe-476f-a563-9bc1c61fc037,Not sure for now,USD,700,22,4,jomzy jor jor,2021-10-10T00:00:00.000Z +3ab96dfd-274a-4097-8e7d-942e58203784,71be6266-81fe-476f-a563-9bc1c61fc037,Not sure for now,USD,700,22,4,jomzy jor jor,2021-10-10T00:00:00.000Z \ No newline at end of file diff --git a/__tests__/api-tests/earningsSuccessfulTest.csv b/__tests__/api-tests/earningsSuccessfulTest.csv index 732ae47..1305196 100644 --- a/__tests__/api-tests/earningsSuccessfulTest.csv +++ b/__tests__/api-tests/earningsSuccessfulTest.csv @@ -1,4 +1,4 @@ -earnings_id,worker_id,phone,currency,amount,payment_confirmation_id,payment_method,paid_at -f3056760-4c36-4cf7-b625-ed656d314794,71be6266-81fe-476f-a563-9bc1c61fc037,Not sure for now,USD,700,2,jomzy jor jor,2021-10-10T00:00:00.000Z -a69e5cec-945f-4693-8a94-01623ee44187,71be6266-81fe-476f-a563-9bc1c61fc037,Not sure for now,USD,700,3,jomzy jor jor,2021-10-10T00:00:00.000Z -61714d24-6131-4296-ae36-30a7199cc645,71be6266-81fe-476f-a563-9bc1c61fc037,Not sure for now,USD,700,4,jomzy jor jor,2021-10-10T00:00:00.000Z \ No newline at end of file +earnings_id,worker_id,phone,currency,amount,captures_count,payment_confirmation_id,payment_method,paid_at +f3056760-4c36-4cf7-b625-ed656d314794,71be6266-81fe-476f-a563-9bc1c61fc037,Not sure for now,USD,700,22,2,jomzy jor jor,2021-10-10T00:00:00.000Z +a69e5cec-945f-4693-8a94-01623ee44187,71be6266-81fe-476f-a563-9bc1c61fc037,Not sure for now,USD,700,22,3,jomzy jor jor,2021-10-10T00:00:00.000Z +61714d24-6131-4296-ae36-30a7199cc645,71be6266-81fe-476f-a563-9bc1c61fc037,Not sure for now,USD,700,22,4,jomzy jor jor,2021-10-10T00:00:00.000Z \ No newline at end of file diff --git a/__tests__/api-tests/seed-data-creation.js b/__tests__/api-tests/seed-data-creation.js index 5269ae0..a96153f 100644 --- a/__tests__/api-tests/seed-data-creation.js +++ b/__tests__/api-tests/seed-data-creation.js @@ -8,6 +8,7 @@ const workerId = '71be6266-81fe-476f-a563-9bc1c61fc037'; const earningsPaymentObject = { worker_id: workerId, amount: 700, + captures_count: 22, payment_confirmation_id: uuid(), payment_method: 'cash', currency: 'USD', @@ -59,14 +60,14 @@ before(async () => { await knex.raw(` INSERT INTO earnings( - id, worker_id, funder_id, amount, currency, calculated_at, consolidation_rule_id, consolidation_period_start, consolidation_period_end, payment_confirmed_by, payment_confirmation_method, status, active, contract_id) + id, worker_id, funder_id, amount, captures_count, currency, calculated_at, consolidation_rule_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(), 1,'single', '${earningsOne.status}', true, '${uuid()}'), - ('${earningsTwo.id}','${workerId}', '${uuid()}', '${earningsPaymentObject.amount}', '${earningsPaymentObject.currency}', now(), '${uuid()}', now(), now(), 1,'single', '${earningsTwo.status}', true, '${uuid()}'), - ('${earningsThree.id}','${workerId}', '${uuid()}', '${earningsPaymentObject.amount}', '${earningsPaymentObject.currency}', now(), '${uuid()}', now(), now(), 1,'single', '${earningsThree.status}', true, '${uuid()}'), - ('${earningsFour.id}','${workerId}', '${uuid()}', '${earningsPaymentObject.amount}', '${earningsPaymentObject.currency}', now(), '${uuid()}', now(), now(), 1,'batch', '${earningsFour.status}', true, '${uuid()}'), - ('${earningsWithCancelledStatus.id}','${workerId}', '${uuid()}', '${earningsPaymentObject.amount}', '${earningsPaymentObject.currency}', now(), '${uuid()}', now(), now(), 1,'single', '${earningsWithCancelledStatus.status}', true, '${uuid()}'), - ('${earningsWithPaidStatus.id}','${workerId}', '${uuid()}', '${earningsPaymentObject.amount}', '${earningsPaymentObject.currency}', now(), '${uuid()}', now(), now(), 1,'single', '${earningsWithPaidStatus.status}', true, '${uuid()}'); + ('${earningsOne.id}','${workerId}', '${uuid()}', '${earningsPaymentObject.amount}', '${earningsPaymentObject.captures_count}', '${earningsPaymentObject.currency}', now(), '${uuid()}', now(), now(), 1,'single', '${earningsOne.status}', true, '${uuid()}'), + ('${earningsTwo.id}','${workerId}', '${uuid()}', '${earningsPaymentObject.amount}','${earningsPaymentObject.captures_count}', '${earningsPaymentObject.currency}', now(), '${uuid()}', now(), now(), 1,'single', '${earningsTwo.status}', true, '${uuid()}'), + ('${earningsThree.id}','${workerId}', '${uuid()}', '${earningsPaymentObject.amount}','${earningsPaymentObject.captures_count}', '${earningsPaymentObject.currency}', now(), '${uuid()}', now(), now(), 1,'single', '${earningsThree.status}', true, '${uuid()}'), + ('${earningsFour.id}','${workerId}', '${uuid()}', '${earningsPaymentObject.amount}','${earningsPaymentObject.captures_count}', '${earningsPaymentObject.currency}', now(), '${uuid()}', now(), now(), 1,'batch', '${earningsFour.status}', true, '${uuid()}'), + ('${earningsWithCancelledStatus.id}','${workerId}', '${uuid()}', '${earningsPaymentObject.amount}','${earningsPaymentObject.captures_count}', '${earningsPaymentObject.currency}', now(), '${uuid()}', now(), now(), 1,'single', '${earningsWithCancelledStatus.status}', true, '${uuid()}'), + ('${earningsWithPaidStatus.id}','${workerId}', '${uuid()}', '${earningsPaymentObject.amount}','${earningsPaymentObject.captures_count}', '${earningsPaymentObject.currency}', now(), '${uuid()}', now(), now(), 1,'single', '${earningsWithPaidStatus.status}', true, '${uuid()}'); `); }); diff --git a/__tests__/api-tests/seed.spec.js b/__tests__/api-tests/seed.spec.js index a3aca16..7ae12af 100644 --- a/__tests__/api-tests/seed.spec.js +++ b/__tests__/api-tests/seed.spec.js @@ -9,7 +9,7 @@ describe("seed", () => { await knex("earnings").truncate(); }) - it.only("seed", async () => { + it.skip("seed", async () => { await seed.seed( '4c46cdae-d15b-454e-a7e4-570c4895b3f8', 'ae7faf5d-46e2-4944-a6f9-5e65986b2e03', diff --git a/server/handlers/earningsHandler.js b/server/handlers/earningsHandler.js index a7532e1..c494354 100644 --- a/server/handlers/earningsHandler.js +++ b/server/handlers/earningsHandler.js @@ -47,6 +47,7 @@ const earningsPatchSchema = Joi.object({ earnings_id: Joi.string().uuid(), worker_id: Joi.string().uuid().required(), amount: Joi.number().required(), + captures_count: Joi.number().required(), currency: Joi.string().required(), payment_confirmation_id: Joi.string().required(), payment_method: Joi.string().required(), diff --git a/server/models/Earnings.js b/server/models/Earnings.js index 9687c6f..37ae918 100644 --- a/server/models/Earnings.js +++ b/server/models/Earnings.js @@ -226,7 +226,7 @@ const getEarnings = const updateEarnings = async (earningsRepo, requestBody) => { const body = { ...requestBody }; - const { worker_id, currency, amount } = body; + const { worker_id, currency, amount, captures_count } = body; // If data is coming from csv file if (body.earnings_id) { @@ -261,6 +261,15 @@ const updateEarnings = async (earningsRepo, requestBody) => { 'The amount specified does not match that of the earning', ); + if (+earnings.captures_count !== +captures_count) { + console.error("earnings", earnings); + console.error("captures_count", captures_count); + throw new HttpError( + 409, + 'The captures_count specified does not match that of the earning', + ); + } + await earningsRepo.update({ ...body, status: 'paid',