Skip to content

Commit

Permalink
Merge pull request #60 from Kpoke/main
Browse files Browse the repository at this point in the history
fix: add location, lat and lon to payload for /grower_accounts
  • Loading branch information
ZavenArra authored Feb 24, 2022
2 parents 795d5ed + 161b89b commit 1a19956
Show file tree
Hide file tree
Showing 10 changed files with 91 additions and 7 deletions.
54 changes: 54 additions & 0 deletions database/migrations/20220221084222-growerAccountLocation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@


let dbm;
let type;
let seed;
const fs = require('fs');
const path = require('path');

let 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) {
const filePath = path.join(__dirname, 'sqls', '20220221084222-growerAccountLocation-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) {
const filePath = path.join(__dirname, 'sqls', '20220221084222-growerAccountLocation-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,3 @@
ALTER TABLE grower_account DROP location;
ALTER TABLE grower_account DROP lat;
ALTER TABLE grower_account DROP lon;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ALTER TABLE grower_account ADD location geometry(POINT, 4326);
ALTER TABLE grower_account ADD lon numeric;
ALTER TABLE grower_account ADD lat numeric;
4 changes: 2 additions & 2 deletions server/__tests__/integration/capture/capture.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ describe('/captures', () => {
.send({ ...capture2, id: capture1.id })
.set('Accept', 'application/json')
.expect(204);
});

it('should confirm number of sent capture created events', async () => {
// added a timer to confirm this because the function call in the API is not 'awaited'
await new Promise((resolve) => setTimeout(resolve, 2000));
const numOfEmittedEvents = await knex('domain_event')
.count()
.where({ status: 'sent' });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ describe('/grower_account', () => {
last_name: grower_account2.last_name,
email: grower_account2.email,
phone: grower_account2.phone,
lat: grower_account2.lat,
lon: grower_account2.lon,
});
});
});
Expand Down
4 changes: 2 additions & 2 deletions server/__tests__/integration/tree/tree.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ describe('/trees', () => {
.send({ ...tree2, id: tree1.id })
.set('Accept', 'application/json')
.expect(204);
});

it('should confirm number of sent capture created events', async () => {
// added a timer to confirm this because the function call in the API is not 'awaited'
await new Promise((resolve) => setTimeout(resolve, 2000));
const numOfEmittedEvents = await knex('domain_event')
.count()
.where({ status: 'sent' });
Expand Down
4 changes: 3 additions & 1 deletion server/__tests__/mock/grower_account1.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@
"first_registration_at": "2021-12-18T13:04:07.371Z",
"email": "[email protected]",
"phone": "12345",
"image_rotation": 14
"image_rotation": 14,
"lat": "14",
"lon": "14"
}
4 changes: 3 additions & 1 deletion server/__tests__/mock/grower_account2.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@
"first_registration_at": "2021-12-18T13:04:07.371Z",
"email": "[email protected]",
"phone": "12345",
"image_rotation": 14
"image_rotation": 14,
"lat": "14",
"lon": "14"
}
10 changes: 9 additions & 1 deletion server/handlers/growerAccountHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ const growerAccountGetQuerySchema = Joi.object({
}).unknown(false);

const growerAccountPostQuerySchema = Joi.object({
wallet: Joi.string().required(),
wallet: Joi.string().required(), //
person_id: Joi.string().uuid(),
organization_id: Joi.string().uuid(),
first_name: Joi.string().required(),
last_name: Joi.string().required(),
lat: Joi.number().required().min(-90).max(90),
lon: Joi.number().required().min(-180).max(180),
email: Joi.string().email().allow(null),
phone: Joi.string().allow(null),
image_url: Joi.string().uri().required(),
Expand Down Expand Up @@ -159,6 +161,9 @@ const growerAccountHandlerPut = async function (req, res, next) {
last_name,
phone,
email,
location,
lat,
lon,
} = growerAccountInsertObject;
const existingGrowerAccount = await growerAccountRepo.getByFilter({
wallet,
Expand All @@ -172,7 +177,10 @@ const growerAccountHandlerPut = async function (req, res, next) {
phone,
first_name,
last_name,
location,
email,
lat,
lon,
updated_at: new Date().toISOString(),
});
} else {
Expand Down
10 changes: 10 additions & 0 deletions server/models/GrowerAccount.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { v4: uuid } = require('uuid');
const { PaginationQueryOptions } = require('./helper');
const knex = require('../../database/connection');

const GrowerAccount = ({
id,
Expand All @@ -10,6 +11,9 @@ const GrowerAccount = ({
last_name,
email,
phone,
lat,
lon,
location,
image_url,
image_rotation,
status,
Expand All @@ -25,6 +29,9 @@ const GrowerAccount = ({
first_name,
last_name,
email,
lat,
lon,
location,
phone,
image_url,
image_rotation,
Expand Down Expand Up @@ -73,6 +80,9 @@ const GrowerAccountInsertObject = (requestBody) =>
...GrowerAccount(requestBody),
id: uuid(),
status: 'active',
location: knex.raw(
`ST_PointFromText('POINT( ${requestBody.lon} ${requestBody.lat}) ', 4326)`,
),
created_at: new Date().toISOString(),
updated_at: new Date().toISOString(),
});
Expand Down

0 comments on commit 1a19956

Please sign in to comment.