Skip to content

Commit

Permalink
feat: update contract query result format
Browse files Browse the repository at this point in the history
  • Loading branch information
gwynndp committed Apr 21, 2023
1 parent 3d59b8d commit 272ec97
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 90 deletions.
65 changes: 30 additions & 35 deletions server/infra/database/ContractRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export default class ContractRepository extends BaseRepository<Contract> {
}
delete parameters.tokenized;

// result.whereNot(`${this.tableName}.status`, 'deleted');
result.whereNot(`${this.tableName}.status`, 'deleted');

whereNotNulls.forEach((whereNot) => {
// to map table names to fields for query
Expand All @@ -38,8 +38,6 @@ export default class ContractRepository extends BaseRepository<Contract> {
default:
result.whereNotNull(whereNot);
}

// result.whereNotNull(whereNot);
});

whereNulls.forEach((whereNull) => {
Expand All @@ -51,7 +49,6 @@ export default class ContractRepository extends BaseRepository<Contract> {
default:
result.whereNull(whereNull);
}
// result.whereNull(whereNull);
});

whereIns.forEach((whereIn) => {
Expand All @@ -73,25 +70,11 @@ export default class ContractRepository extends BaseRepository<Contract> {
delete filterObject.endDate;
}

// if (filterObject.tag_id) {
// filterObject[`treetracker.capture_tag.tag_id`] = filterObject.tag_id;
// delete filterObject.tag_id;
// }

if (filterObject.id) {
result.where(`${this.tableName}.id`, '=', filterObject.id);
delete filterObject.id;
}

// if (filterObject.reference_id) {
// result.where(
// `${this.tableName}.reference_id`,
// '=',
// filterObject.reference_id,
// );
// delete filterObject.reference_id;
// }

if (filterObject.organization_id) {
result.where(`${this.tableName}.growing_organization_id`, 'in', [
...filterObject.organization_id,
Expand All @@ -106,30 +89,35 @@ export default class ContractRepository extends BaseRepository<Contract> {
const knex = this.session.getDB();
const { sort, ...filter } = filterCriteria;

// there are two joins to connect the capture to the token and to the wallet right now so that we can double check our data entries
// the current data entered in the tables doesn't match up so there are some mismatches
let promise = knex
.select(
knex.raw(
`
row_to_json(contract.*) AS contract,
${this.tableName}.id,
${this.tableName}.status,
${this.tableName}.notes,
${this.tableName}.created_at,
${this.tableName}.updated_at,
${this.tableName}.signed_at,
${this.tableName}.closed_at,
${this.tableName}.listed,
row_to_json(agreement.*) AS agreement,
row_to_json(grower_account.*) AS worker,
row_to_json(stakeholder.*) AS stakeholder
FROM contracts.contract AS contract
FROM ${this.tableName}
LEFT JOIN contracts.agreement AS agreement
ON agreement.id = contract.agreement_id
ON agreement.id = ${this.tableName}.agreement_id
LEFT JOIN stakeholder.stakeholder AS stakeholder
ON stakeholder.id = agreement.growing_organization_id
LEFT JOIN treetracker.grower_account AS grower_account
ON grower_account.id = contract.worker_id
ON grower_account.id = ${this.tableName}.worker_id
`,
),
)
.where((builder) => this.filterWhereBuilder(filter, builder));

promise = promise.orderBy(
sort?.order_by || `${this.tableName}.id`,
`${this.tableName}.${sort?.order_by}` || `${this.tableName}.id`,
sort?.order || 'desc',
);

Expand All @@ -154,13 +142,13 @@ export default class ContractRepository extends BaseRepository<Contract> {
.select(
knex.raw(
`COUNT(*) AS count
FROM contracts.contract AS contract
FROM ${this.tableName}
LEFT JOIN contracts.agreement AS agreement
ON agreement.id = contract.agreement_id
ON agreement.id = ${this.tableName}.agreement_id
LEFT JOIN stakeholder.stakeholder AS stakeholder
ON stakeholder.id = agreement.growing_organization_id
LEFT JOIN treetracker.grower_account AS grower_account
ON grower_account.id = contract.worker_id
ON grower_account.id = ${this.tableName}.worker_id
`,
),
)
Expand All @@ -174,17 +162,24 @@ export default class ContractRepository extends BaseRepository<Contract> {
.getDB()
.select(
this.session.getDB().raw(`
contract.*,
agreement.*,
grower_account.*,
stakeholder.*
FROM contracts.contract AS contract
${this.tableName}.id,
${this.tableName}.status,
${this.tableName}.notes,
${this.tableName}.created_at,
${this.tableName}.updated_at,
${this.tableName}.signed_at,
${this.tableName}.closed_at,
${this.tableName}.listed,
row_to_json(agreement.*) AS agreement,
row_to_json(grower_account.*) AS worker,
row_to_json(stakeholder.*) AS stakeholder
FROM ${this.tableName}
LEFT JOIN contracts.agreement AS agreement
ON agreement.id = contract.agreement_id
ON agreement.id = ${this.tableName}.agreement_id
LEFT JOIN stakeholder.stakeholder AS stakeholder
ON stakeholder.id = agreement.growing_organization_id
LEFT JOIN treetracker.grower_account AS grower_account
ON grower_account.id = contract.worker_id
ON grower_account.id = ${this.tableName}.worker_id
`),
)
.where(`${this.tableName}.id`, id)
Expand Down
11 changes: 0 additions & 11 deletions server/interfaces/ContractFilter.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
import DbModel from './DbModel';

interface ContractFilter extends DbModel {
// organization_id?: Array<string> | undefined;
// grower_account_id?: string | undefined;
// id?: string | undefined;
// tree_id?: string | undefined;
// species_id?: string | undefined;
// tag?: string | undefined;
// device_identifier?: string | undefined;
// wallet?: string | undefined;
// token_id?: string | undefined;
// tokenized?: string | undefined;

id?: number | undefined;
agreement_id?: number | undefined;
worker_id?: number | undefined;
Expand Down
60 changes: 16 additions & 44 deletions server/routers/contractsRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,20 @@ router.get(
handlerWrapper(async (req, res) => {
const query = queryFormatter(req);

// verify filter values
Joi.assert(
query,
Joi.object().keys({
// capture filters
grower_account_id: Joi.string().uuid(),
// grower_account_id: Joi.string().uuid(),
organization_id: Joi.array(),
limit: Joi.number().integer().min(1).max(20000),
offset: Joi.number().integer().min(0),
startDate: Joi.string().regex(/^\d{4}-\d{2}-\d{2}$/),
endDate: Joi.string().regex(/^\d{4}-\d{2}-\d{2}$/),
// contract table filters
id: Joi.string().uuid(),
// reference_id: Joi.string(),
// tree_id: Joi.string().uuid(),
// species_id: Joi.string().uuid(),
// tag_id: Joi.string().uuid(),
// device_identifier: Joi.string(),
wallet: Joi.string(),
// tokenized: Joi.string(),
// order_by: Joi.string(),
// order: Joi.string(),
token_id: Joi.string().uuid(),
// contract filters
agreement_id: Joi.string().uuid(),
worker_id: Joi.string().uuid(), // grower_account_id?
listed: Joi.boolean(),

// organization_id: Joi.array(),
startDate: Joi.string().regex(/^\d{4}-\d{2}-\d{2}$/),
endDate: Joi.string().regex(/^\d{4}-\d{2}-\d{2}$/),
// defaults
limit: Joi.number().integer().min(1).max(20000),
offset: Joi.number().integer().min(0),
whereNulls: Joi.array(),
whereNotNulls: Joi.array(),
whereIns: Joi.array(),
Expand Down Expand Up @@ -72,35 +58,21 @@ router.get(
'/',
handlerWrapper(async (req, res) => {
const query = queryFormatter(req);
console.log('************************* /contract');

// verify filter values
Joi.assert(
query,
Joi.object().keys({
// capture filters
// grower_account_id: Joi.string().uuid(),
organization_id: Joi.array(),
limit: Joi.number().integer().min(1).max(20000),
offset: Joi.number().integer().min(0),
startDate: Joi.string().regex(/^\d{4}-\d{2}-\d{2}$/),
endDate: Joi.string().regex(/^\d{4}-\d{2}-\d{2}$/),
// contract table filters
id: Joi.string().uuid(),
// reference_id: Joi.string(),
// tree_id: Joi.string().uuid(),
// species_id: Joi.string().uuid(),
// tag_id: Joi.string().uuid(),
// device_identifier: Joi.string(),
wallet: Joi.string(),
// tokenized: Joi.string(),
// order_by: Joi.string(),
// order: Joi.string(),
token_id: Joi.string().uuid(),
// contract filters
agreement_id: Joi.string().uuid(),
worker_id: Joi.string().uuid(), // grower_account_id?
listed: Joi.boolean(),

// organization_id: Joi.array(),
startDate: Joi.string().regex(/^\d{4}-\d{2}-\d{2}$/),
endDate: Joi.string().regex(/^\d{4}-\d{2}-\d{2}$/),
// defaults
limit: Joi.number().integer().min(1).max(20000),
offset: Joi.number().integer().min(0),
whereNulls: Joi.array(),
whereNotNulls: Joi.array(),
whereIns: Joi.array(),
Expand All @@ -110,7 +82,7 @@ router.get(
limit = 25,
offset = 0,
order = 'desc',
order_by = 'contract.id',
order_by = 'id',
...rest
} = query;

Expand All @@ -120,7 +92,7 @@ router.get(
const result = await exe({ ...rest, sort }, { limit, offset });
const count = await ContractModel.getCount(repo)({ ...rest });
res.send({
Contracts: result,
contracts: result,
total: Number(count),
offset,
limit,
Expand Down

0 comments on commit 272ec97

Please sign in to comment.