From 776c09aabd45a6614404184a47f9165e859ba778 Mon Sep 17 00:00:00 2001 From: Daniel Olojakpoke Date: Sat, 20 Aug 2022 18:34:44 +0100 Subject: [PATCH] fix: rabbitmq captures --- .../20220820032302-add-reference-id-unique.js | 53 +++++++++++++++++++ ...820032302-add-reference-id-unique-down.sql | 1 + ...20820032302-add-reference-id-unique-up.sql | 1 + server/app.js | 3 -- server/handlers/captureHandler/schemas.js | 1 - server/models/Capture.js | 40 +++++++------- server/repositories/CaptureRepository.js | 8 ++- server/repositories/EventRepository.js | 6 +-- server/services/EventHandlerService.js | 14 ----- 9 files changed, 86 insertions(+), 41 deletions(-) create mode 100644 database/migrations/20220820032302-add-reference-id-unique.js create mode 100644 database/migrations/sqls/20220820032302-add-reference-id-unique-down.sql create mode 100644 database/migrations/sqls/20220820032302-add-reference-id-unique-up.sql delete mode 100644 server/services/EventHandlerService.js diff --git a/database/migrations/20220820032302-add-reference-id-unique.js b/database/migrations/20220820032302-add-reference-id-unique.js new file mode 100644 index 00000000..74804560 --- /dev/null +++ b/database/migrations/20220820032302-add-reference-id-unique.js @@ -0,0 +1,53 @@ +'use strict'; + +var dbm; +var type; +var seed; +var fs = require('fs'); +var path = require('path'); +var Promise; + +/** + * We receive the dbmigrate dependency from dbmigrate initially. + * This enables us to not have to rely on NODE_PATH. + */ +exports.setup = function(options, seedLink) { + dbm = options.dbmigrate; + type = dbm.dataType; + seed = seedLink; + Promise = options.Promise; +}; + +exports.up = function(db) { + var filePath = path.join(__dirname, 'sqls', '20220820032302-add-reference-id-unique-up.sql'); + return new Promise( function( resolve, reject ) { + fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){ + if (err) return reject(err); + console.log('received data: ' + data); + + resolve(data); + }); + }) + .then(function(data) { + return db.runSql(data); + }); +}; + +exports.down = function(db) { + var filePath = path.join(__dirname, 'sqls', '20220820032302-add-reference-id-unique-down.sql'); + return new Promise( function( resolve, reject ) { + fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){ + if (err) return reject(err); + console.log('received data: ' + data); + + resolve(data); + }); + }) + .then(function(data) { + return db.runSql(data); + }); +}; + +exports._meta = { + "version": 1 +}; diff --git a/database/migrations/sqls/20220820032302-add-reference-id-unique-down.sql b/database/migrations/sqls/20220820032302-add-reference-id-unique-down.sql new file mode 100644 index 00000000..adca538c --- /dev/null +++ b/database/migrations/sqls/20220820032302-add-reference-id-unique-down.sql @@ -0,0 +1 @@ +ALTER TABLE capture DROP CONSTRAINT capture_reference_id_unique; \ No newline at end of file diff --git a/database/migrations/sqls/20220820032302-add-reference-id-unique-up.sql b/database/migrations/sqls/20220820032302-add-reference-id-unique-up.sql new file mode 100644 index 00000000..f12c6ef7 --- /dev/null +++ b/database/migrations/sqls/20220820032302-add-reference-id-unique-up.sql @@ -0,0 +1 @@ +ALTER TABLE capture ADD CONSTRAINT capture_reference_id_unique UNIQUE (reference_id); \ No newline at end of file diff --git a/server/app.js b/server/app.js index 78c2919d..0b81c553 100644 --- a/server/app.js +++ b/server/app.js @@ -6,7 +6,6 @@ const HttpError = require('./utils/HttpError'); const { errorHandler } = require('./utils/utils'); const helper = require('./utils/utils'); const router = require('./routes'); -const registerEventHandlers = require('./services/EventHandlerService'); const app = express(); @@ -51,6 +50,4 @@ app.get('*', function (req, res) { res.status(200).send(version); }); -registerEventHandlers(); - module.exports = app; diff --git a/server/handlers/captureHandler/schemas.js b/server/handlers/captureHandler/schemas.js index bef6c6cf..29b43ca0 100644 --- a/server/handlers/captureHandler/schemas.js +++ b/server/handlers/captureHandler/schemas.js @@ -1,7 +1,6 @@ const Joi = require('joi'); const capturePostSchema = Joi.object({ - id: Joi.string().uuid().required(), session_id: Joi.string().uuid().required(), image_url: Joi.string().uri().required(), lat: Joi.number().required().min(-90).max(90).required(), diff --git a/server/models/Capture.js b/server/models/Capture.js index 321c819c..d38abffc 100644 --- a/server/models/Capture.js +++ b/server/models/Capture.js @@ -11,6 +11,7 @@ class Capture { static Capture({ id, + reference_id, tree_id = undefined, image_url, lat, @@ -30,6 +31,7 @@ class Capture { }) { return Object.freeze({ id, + reference_id, image_url, planting_organization_id, created_at, @@ -51,7 +53,7 @@ class Capture { } static CaptureCreated({ - id, + reference_id, lat, lon, grower_account_id, @@ -59,7 +61,8 @@ class Capture { captured_at, }) { return Object.freeze({ - id, + reference_id, + approved: true, type: 'CaptureCreated', lat, lon, @@ -149,6 +152,16 @@ class Capture { return this._response(capture); } + async getCaptureByReferenceId(referenceId) { + const captures = await this._captureRepository.getByFilter({ + parameters: { reference_id: referenceId }, + }); + + const [capture = {}] = captures; + + return this._response(capture); + } + async createCapture(captureObject) { const eventRepo = new EventRepository(this._session); @@ -165,9 +178,13 @@ class Capture { updated_at: new Date().toISOString(), created_at: new Date().toISOString(), }; - const existingCapture = await this.getCaptureById(newCapture.id); + const existingCapture = await this.getCaptureByReferenceId( + newCapture.reference_id, + ); if (existingCapture?.id) { - const domainEvent = await eventRepo.getDomainEvent(newCapture.id); + const domainEvent = await eventRepo.getDomainEvent( + newCapture.reference_id, + ); if (domainEvent.status !== 'sent') { return { domainEvent, @@ -204,21 +221,6 @@ class Capture { return this._response(updatedCapture); } - - async applyVerification(verifyCaptureProcessed) { - if (verifyCaptureProcessed.approved) { - await this._captureRepository.update({ - id: verifyCaptureProcessed.id, - status: 'approved', - }); - } else { - await this._captureRepository.update({ - id: verifyCaptureProcessed.id, - status: 'rejected', - rejection_reason: verifyCaptureProcessed.rejection_reason, - }); - } - } } module.exports = Capture; diff --git a/server/repositories/CaptureRepository.js b/server/repositories/CaptureRepository.js index a6112764..05f19442 100644 --- a/server/repositories/CaptureRepository.js +++ b/server/repositories/CaptureRepository.js @@ -9,7 +9,12 @@ class CaptureRepository extends BaseRepository { _filterWhereBuilder(object, builder) { const result = builder; - const { parameters, whereNulls = [], whereNotNulls = [], whereIns = [] } = { + const { + parameters, + whereNulls = [], + whereNotNulls = [], + whereIns = [], + } = { ...object, }; result.whereNot(`${this._tableName}.status`, 'deleted'); @@ -54,6 +59,7 @@ class CaptureRepository extends BaseRepository { knex.raw( ` id, + reference_id, tree_id, planting_organization_id, image_url, diff --git a/server/repositories/EventRepository.js b/server/repositories/EventRepository.js index 72decc68..0f61356a 100644 --- a/server/repositories/EventRepository.js +++ b/server/repositories/EventRepository.js @@ -11,11 +11,11 @@ class EventRepository extends BaseRepository { return super.create(domainEvent); } - async getDomainEvent(payloadId) { + async getDomainEvent(payloadReferenceId) { const data = await this._session .getDB() - .raw(`select * from domain_event where payload ->> 'id' = ?;`, [ - payloadId, + .raw(`select * from domain_event where payload ->> 'reference_id' = ?;`, [ + payloadReferenceId, ]); return data.rows[0]; diff --git a/server/services/EventHandlerService.js b/server/services/EventHandlerService.js deleted file mode 100644 index 69a317cf..00000000 --- a/server/services/EventHandlerService.js +++ /dev/null @@ -1,14 +0,0 @@ -// const Session = require('../database/Session'); -// // const { subscribe } = require('../messaging/RabbitMQMessaging'); - -// const EventRepository = require('../repositories/EventRepository'); -// const CaptureRepository = require('../repositories/CaptureRepository'); - -// const { DomainEvent, receiveEvent } = require('../models/DomainEvent'); -// const { applyVerification } = require('../models/Capture'); - -const registerEventHandlers = () => { - // subscribe('admin-verification', handleVerifyCaptureProcessed); -}; - -module.exports = registerEventHandlers;