Skip to content

Commit

Permalink
feat: fix merge errors
Browse files Browse the repository at this point in the history
  • Loading branch information
gwynndp committed Nov 13, 2023
1 parent cf1f03b commit f5516ed
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 7 deletions.
14 changes: 14 additions & 0 deletions server/infra/database/OrganizationRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,18 @@ export default class OrganizationRepository extends BaseRepository<Organization>
);
return objectPatched;
}

async getByIds(ids: Array<number>, options: FilterOptions) {
const { limit, offset } = options;
const sql = `
SELECT
*
FROM entity
WHERE id IN (${ids})
LIMIT ${limit}
OFFSET ${offset}
`;
const object = await this.session.getDB().raw(sql);
return object.rows;
}
}
14 changes: 14 additions & 0 deletions server/infra/database/OrganizationRepositoryV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,20 @@ export default class OrganizationRepositoryV2 extends BaseRepository<Organizatio
return objectPatched;
}

async getByIds(ids: Array<string>, options: FilterOptions) {
const { limit = 20, offset = 0 } = options;

const result = await this.session
.getDB()
.select('*')
.from(this.tableName)
.whereIn('id', ids)
.offset(offset)
.limit(limit);

return result;
}

// async getByGrowerId(id: string | number) {
// const object = await this.session
// .getDB()
Expand Down
12 changes: 10 additions & 2 deletions server/models/Organization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import Organization from 'interfaces/Organization';
import { delegateRepository } from '../infra/database/delegateRepository';
import OrganizationRepository from '../infra/database/OrganizationRepository';

type Filter = Partial<{ planter_id: number; organization_id: number }>;
type Filter = Partial<{
planter_id: number;
organization_id: number;
ids: Array<number>;
}>;

function getByFilter(
organizationRepository: OrganizationRepository,
Expand All @@ -18,7 +22,11 @@ function getByFilter(
);
return trees;
}

if (filter?.ids?.length) {
log.warn('using ids filter...');
const trees = await organizationRepository.getByIds(filter.ids, options);
return trees;
}
const trees = await organizationRepository.getByFilter(filter, options);
return trees;
};
Expand Down
6 changes: 6 additions & 0 deletions server/models/OrganizationV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ type Filter = Partial<{
planter_id: number;
organization_id: number;
grower_id: string;
ids: Array<string>;
}>;

function getByFilter(
Expand All @@ -30,6 +31,11 @@ function getByFilter(
);
return trees;
}
if (filter?.ids?.length) {
log.warn('using ids filter...');
const trees = await organizationRepository.getByIds(filter.ids, options);
return trees;
}
const trees = await organizationRepository.getByFilter(filter, options);
return trees;
};
Expand Down
27 changes: 24 additions & 3 deletions server/routers/organizationsRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ import OrganizationRepository from '../infra/database/OrganizationRepository';
import Session from '../infra/database/Session';
import OrganizationModel from '../models/Organization';

type Filter = Partial<{ planter_id: number; organization_id: number }>;
type Filter = Partial<{
planter_id: number;
organization_id: number;
ids: Array<number>;
stakeholder_uuid: string;
}>;

const router = express.Router();

Expand Down Expand Up @@ -47,20 +52,36 @@ router.get(
router.get(
'/',
handlerWrapper(async (req, res) => {
const query = { ...req.query };
query.ids = JSON.parse(req.query.ids);
Joi.assert(
req.query,
query,
Joi.object().keys({
planter_id: Joi.number().integer().min(0),
ids: Joi.array(),
stakeholder_uuid: Joi.string().uuid(),
limit: Joi.number().integer().min(1).max(1000),
offset: Joi.number().integer().min(0),
}),
);
const { limit = 20, offset = 0, planter_id } = req.query;
const {
limit = 20,
offset = 0,
planter_id,
ids = [],
stakeholder_uuid,
} = query;
const repo = new OrganizationRepository(new Session());
const filter: Filter = {};
if (planter_id) {
filter.planter_id = planter_id;
}
if (stakeholder_uuid) {
filter.stakeholder_uuid = stakeholder_uuid;
}
if (ids.length) {
filter.ids = ids;
}
const result = await OrganizationModel.getByFilter(repo)(filter, {
limit,
offset,
Expand Down
11 changes: 9 additions & 2 deletions server/routers/organizationsRouterV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type Filter = Partial<{
planter_id: number;
organization_id: number;
grower_id: string;
ids: Array<string>;
}>;

const router = express.Router();
Expand Down Expand Up @@ -51,23 +52,29 @@ router.get(
router.get(
'/',
handlerWrapper(async (req, res) => {
const query = { ...req.query };
query.ids = JSON.parse(req.query.ids);
Joi.assert(
req.query,
query,
Joi.object().keys({
planter_id: Joi.number().integer().min(0),
grower_id: Joi.string().guid(),
ids: Joi.array().items(Joi.string().uuid()),
limit: Joi.number().integer().min(1).max(1000),
offset: Joi.number().integer().min(0),
}),
);
const { limit = 20, offset = 0, planter_id, grower_id } = req.query;
const { limit = 20, offset = 0, planter_id, grower_id, ids = [] } = query;
const repo = new OrganizationRepositoryV2(new Session());
const filter: Filter = {};
if (planter_id) {
filter.planter_id = planter_id;
} else if (grower_id) {
filter.grower_id = grower_id;
}
if (ids.length) {
filter.ids = ids;
}
const result = await OrganizationModel.getByFilter(repo)(filter, {
limit,
offset,
Expand Down

0 comments on commit f5516ed

Please sign in to comment.