diff --git a/api-tests/integration/capture/capture.spec.js b/api-tests/integration/capture/capture.spec.js index 43948527..d111936e 100644 --- a/api-tests/integration/capture/capture.spec.js +++ b/api-tests/integration/capture/capture.spec.js @@ -61,6 +61,11 @@ describe('/captures', () => { await knex('domain_event').insert({ ...domain_event2 }); }); + after(async () => { + await knex('capture').del(); + await knex('domain_event').del(); + }); + it('should create a capture', async () => { const res = await request(app) .post(`/captures`) @@ -98,7 +103,7 @@ describe('/captures', () => { it('should resend capture created event if it wasnt successful last time and capture already exists', async () => { await request(app) .post(`/captures`) - .send({ ...capture2, id: capture1.id }) + .send({ ...capture2, reference_id: capture1.reference_id }) .set('Accept', 'application/json') .expect(200); @@ -109,30 +114,31 @@ describe('/captures', () => { .where({ status: 'sent' }); expect(+numOfEmittedEvents[0].count).to.eql(2); }); - - after(async () => { - await knex('capture').del(); - await knex('domain_event').del(); - }); }); describe('PATCH', () => { + const captureId = uuid.v4(); before(async () => { await addCapture({ ...capture2, + id: captureId, estimated_geometric_location: 'POINT(50 50)', updated_at: '2022-01-01T11:11:11.000Z', attributes: { entries: attributes.attributes }, }); }); + after(async () => { + await knex('capture').del(); + }); + it('should update a capture', async () => { const updates = { tree_id: tree1.id, }; const res = await request(app) - .patch(`/captures/${capture2.id}`) + .patch(`/captures/${captureId}`) .send(updates) .set('Accept', 'application/json') .expect(200); @@ -146,13 +152,10 @@ describe('/captures', () => { tree_id: updates.tree_id, }); }); - - after(async () => { - await knex('capture').del(); - }); }); describe('GET', () => { + const captureId = uuid.v4(); before(async () => { await addCapture({ ...capture1, @@ -162,6 +165,7 @@ describe('/captures', () => { }); await addCapture({ ...capture2, + id: captureId, estimated_geometric_location: 'POINT(50 50)', updated_at: '2022-02-01 11:11:11', attributes: { entries: attributes.attributes }, @@ -192,14 +196,12 @@ describe('/captures', () => { .expect(200); expect(result.body.captures.length).to.eql(1); expect(result.body.query.count).to.eql(1); - expect(result.body.captures[0].id).to.eql( - 'd2c69205-b13f-4ab6-bb5e-33dc504fa0c2', - ); + expect(result.body.captures[0].id).to.eql(captureId); }); it('should delete a capture', async () => { await request(app) - .patch(`/captures/${capture2.id}`) + .patch(`/captures/${captureId}`) .send({ status: 'deleted' }) .set('Accept', 'application/json') .expect(200); @@ -214,7 +216,7 @@ describe('/captures', () => { }); describe('/captures/capture_id/tags', () => { - const captureId = capture2.id; + const captureId = uuid.v4(); before(async () => { await addCapture({ @@ -224,6 +226,7 @@ describe('/captures', () => { }); await addCapture({ ...capture2, + id: captureId, estimated_geometric_location: 'POINT(50 50)', updated_at: '2022-02-01 11:11:11', }); diff --git a/api-tests/mock/capture1.json b/api-tests/mock/capture1.json index 668b0c9b..42247a27 100644 --- a/api-tests/mock/capture1.json +++ b/api-tests/mock/capture1.json @@ -8,4 +8,4 @@ "image_url": "http://xxx", "planting_organization_id": "3876b7df-0002-4bc7-adfa-097db2080311", "captured_at": "2022-01-01 11:11:11" -} \ No newline at end of file +} diff --git a/api-tests/mock/capture2.json b/api-tests/mock/capture2.json index b35ce209..303adb1d 100644 --- a/api-tests/mock/capture2.json +++ b/api-tests/mock/capture2.json @@ -1,5 +1,4 @@ { - "id": "d2c69205-b13f-4ab6-bb5e-33dc504fa0c2", "reference_id": 215606, "session_id": "f97a0338-1d6a-4f19-a067-a7f0b573e3cd", "image_url": "https://treetracker-dev-images.s3.eu-central-1.amazonaws.com/2020.05.15.16.18.46_37.42138089_-121.87534965_92b10ff4-4d37-43e9-8dd7-ed79e23d1102_IMG_20200511_150539_503676129060555307.jpg", diff --git a/api-tests/mock/domain_event2.json b/api-tests/mock/domain_event2.json index 73daf994..663bcd4c 100644 --- a/api-tests/mock/domain_event2.json +++ b/api-tests/mock/domain_event2.json @@ -1,7 +1,8 @@ { "id": "7cffd8c0-78fb-4cc4-ba9c-1a2856d459d7", "payload": { - "id": "c02a5ae6-3727-11ec-8d3d-0242ac130003" + "id": "c02a5ae6-3727-11ec-8d3d-0242ac130003", + "reference_id": 1 }, "status": "raised", "created_at": "2021-05-04 11:24:43", diff --git a/server/models/Capture.js b/server/models/Capture.js index cc407197..3f5c482c 100644 --- a/server/models/Capture.js +++ b/server/models/Capture.js @@ -53,6 +53,7 @@ class Capture { } static CaptureCreated({ + id, reference_id, lat, lon, @@ -61,6 +62,7 @@ class Capture { captured_at, }) { return Object.freeze({ + id, reference_id, approved: true, type: 'CaptureCreated', @@ -184,8 +186,8 @@ class Capture { newCapture.reference_id, ); if (existingCapture?.id) { - const domainEvent = await eventRepo.getDomainEvent( - newCapture.reference_id, + const domainEvent = await eventRepo.getCaptureDomainEvent( + existingCapture.reference_id, ); if (domainEvent.status !== 'sent') { return { diff --git a/server/models/tree.js b/server/models/tree.js index b358ff9a..b51eeb8f 100644 --- a/server/models/tree.js +++ b/server/models/tree.js @@ -85,7 +85,7 @@ class Tree { }); const [existingTree] = existingTrees; if (existingTree) { - const domainEvent = await eventRepo.getDomainEvent(newTree.id); + const domainEvent = await eventRepo.getTreeDomainEvent(newTree.id); if (domainEvent.status !== 'sent') { return { domainEvent, tree: existingTree, eventRepo, status: 200 }; } diff --git a/server/repositories/EventRepository.js b/server/repositories/EventRepository.js index 0f61356a..44567a76 100644 --- a/server/repositories/EventRepository.js +++ b/server/repositories/EventRepository.js @@ -11,7 +11,7 @@ class EventRepository extends BaseRepository { return super.create(domainEvent); } - async getDomainEvent(payloadReferenceId) { + async getCaptureDomainEvent(payloadReferenceId) { const data = await this._session .getDB() .raw(`select * from domain_event where payload ->> 'reference_id' = ?;`, [ @@ -20,6 +20,16 @@ class EventRepository extends BaseRepository { return data.rows[0]; } + + async getTreeDomainEvent(payloadId) { + const data = await this._session + .getDB() + .raw(`select * from domain_event where payload ->> 'id' = ?;`, [ + payloadId, + ]); + + return data.rows[0]; + } } module.exports = EventRepository;