Skip to content

Commit

Permalink
Merge pull request #115 from Kpoke/fix/planter
Browse files Browse the repository at this point in the history
add planter reference id
  • Loading branch information
Kpoke authored Nov 7, 2022
2 parents 71f2af3 + 3b2fb73 commit b6b8d82
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 3 deletions.
5 changes: 4 additions & 1 deletion __tests__/integration/grower_account/grower_account.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ describe('/grower_account', () => {
gender: 'female',
email: 'name@email.com',
phone: '1234567',
about: "about",
about: 'about',
image_url: 'https://www.himage.com',
image_rotation: 44,
};

after(async () => {
await knex('grower_account_org').del();
await knex('grower_account').del();
await knex('planter').del();
});

describe('POST', () => {
Expand All @@ -39,6 +40,7 @@ describe('/grower_account', () => {
expect(res.body).include({
...grower_account1,
});
expect(typeof res.body.reference_id).eql('number');
expect(res.body.organizations.length).to.eql(0);

const res2 = await request(app)
Expand All @@ -50,6 +52,7 @@ describe('/grower_account', () => {
expect(res2.body).include({
...grower_account2,
});
expect(typeof res2.body.reference_id).eql('number');
expect(res2.body.organizations.length).to.eql(0);
});

Expand Down
23 changes: 23 additions & 0 deletions __tests__/integration/grower_account/legacy-planter-creation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const knex = require('../../../server/infra/database/knex');

before(async () => {
await knex.raw(
`
CREATE TABLE IF NOT EXISTS public.planter
(
id serial,
first_name character varying(30) COLLATE pg_catalog."default" NOT NULL,
last_name character varying(30) COLLATE pg_catalog."default" NOT NULL,
email character varying COLLATE pg_catalog."default",
organization character varying COLLATE pg_catalog."default",
phone text COLLATE pg_catalog."default",
pwd_reset_required boolean DEFAULT false,
image_url character varying COLLATE pg_catalog."default",
person_id integer,
organization_id integer,
image_rotation integer,
grower_account_uuid uuid,
CONSTRAINT planter_id_key PRIMARY KEY (id)
)`,
);
});
53 changes: 53 additions & 0 deletions database/migrations/20221107062128-grower-account-reference-id.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', '20221107062128-grower-account-reference-id-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', '20221107062128-grower-account-reference-id-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 grower_account DROP reference_id;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE grower_account ADD reference_id int;
2 changes: 1 addition & 1 deletion server/handlers/growerAccountHandler/schemas.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ 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(),
Expand Down
18 changes: 17 additions & 1 deletion server/models/GrowerAccount.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
const knex = require('../infra/database/knex');
const GrowerAccountRepository = require('../repositories/GrowerAccountRepository');
const LegacyPlanterRepository = require('../repositories/Legacy/PlanterRepository');

class GrowerAccount {
constructor(session) {
this._session = session;
this._growerAccountRepository = new GrowerAccountRepository(session);
}

static GrowerAccount({
id,
reference_id,
wallet,
person_id,
organization_id,
Expand All @@ -31,6 +34,7 @@ class GrowerAccount {
}) {
return Object.freeze({
id,
reference_id,
wallet,
person_id,
organization_id,
Expand Down Expand Up @@ -99,13 +103,25 @@ class GrowerAccount {
let [growerAccount] = existingGrowerAccount;

if (!growerAccount) {
const planterRepository = new LegacyPlanterRepository(this._session);
status = 201;
growerAccount = await this._growerAccountRepository.create({
const createdGrowerAccount = await this._growerAccountRepository.create({
location: knex.raw(
`ST_PointFromText('POINT( ${growerAccountToCreate.lon} ${growerAccountToCreate.lat}) ', 4326)`,
),
...growerAccountToCreate,
});
const planter = await planterRepository.create({
first_name: createdGrowerAccount.first_name,
last_name: createdGrowerAccount.last_name,
phone: createdGrowerAccount.phone,
email: createdGrowerAccount.email,
grower_account_uuid: createdGrowerAccount.id,
});
growerAccount = await this._growerAccountRepository.update({
id: createdGrowerAccount.id,
reference_id: planter.id,
});
}

return { status, growerAccount: this._response(growerAccount) };
Expand Down
1 change: 1 addition & 0 deletions server/repositories/GrowerAccountRepository.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class GrowerAccountRepository extends BaseRepository {
.getDB()
.select(
'grower_account.id',
'grower_account.reference_id',
'grower_account.wallet',
'grower_account.person_id',
'grower_account.organization_id',
Expand Down
11 changes: 11 additions & 0 deletions server/repositories/Legacy/PlanterRepository.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const BaseRepository = require('../BaseRepository');

class LegacyPlanterRepository extends BaseRepository {
constructor(session) {
super('planter', session);
this._tableName = 'planter';
this._session = session;
}
}

module.exports = LegacyPlanterRepository;

0 comments on commit b6b8d82

Please sign in to comment.