Skip to content

Commit

Permalink
fix: rabbitmq captures
Browse files Browse the repository at this point in the history
Kpoke committed Aug 20, 2022

Verified

This commit was signed with the committer’s verified signature.
Kpoke Olojakpoke Daniel
1 parent ba1a9b8 commit 776c09a
Showing 9 changed files with 86 additions and 41 deletions.
53 changes: 53 additions & 0 deletions database/migrations/20220820032302-add-reference-id-unique.js
Original file line number Diff line number Diff line change
@@ -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
};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE capture DROP CONSTRAINT capture_reference_id_unique;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE capture ADD CONSTRAINT capture_reference_id_unique UNIQUE (reference_id);
3 changes: 0 additions & 3 deletions server/app.js
Original file line number Diff line number Diff line change
@@ -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;
1 change: 0 additions & 1 deletion server/handlers/captureHandler/schemas.js
Original file line number Diff line number Diff line change
@@ -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(),
40 changes: 21 additions & 19 deletions server/models/Capture.js
Original file line number Diff line number Diff line change
@@ -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,15 +53,16 @@ class Capture {
}

static CaptureCreated({
id,
reference_id,
lat,
lon,
grower_account_id,
attributes,
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;
8 changes: 7 additions & 1 deletion server/repositories/CaptureRepository.js
Original file line number Diff line number Diff line change
@@ -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,
6 changes: 3 additions & 3 deletions server/repositories/EventRepository.js
Original file line number Diff line number Diff line change
@@ -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];
14 changes: 0 additions & 14 deletions server/services/EventHandlerService.js

This file was deleted.

0 comments on commit 776c09a

Please sign in to comment.