|
| 1 | +import { MixinTarget } from '@loopback/core'; |
| 2 | +import { CrudRepository, Model } from '@loopback/repository'; |
| 3 | +import expect from 'expect-runtime'; |
| 4 | +import { buildFilterQuery } from '../js/buildFilterQuery'; |
| 5 | +import { utils } from '../js/utils'; |
| 6 | + |
| 7 | +export function UtilsRepositoryMixin< |
| 8 | + M extends Model, |
| 9 | + R extends MixinTarget<CrudRepository<M>>, |
| 10 | +>(superClass: R) { |
| 11 | + return class extends superClass { |
| 12 | + [x: string]: any; |
| 13 | + // put the shared code here |
| 14 | + async getEntityIdsByOrganizationId( |
| 15 | + organizationId: number, |
| 16 | + ): Promise<Array<number>> { |
| 17 | + expect(organizationId).number(); |
| 18 | + expect(this).property('execute').defined(); |
| 19 | + const result = await this.execute( |
| 20 | + `select * from getEntityRelationshipChildren(${organizationId})`, |
| 21 | + [], |
| 22 | + ); |
| 23 | + return result.map((e) => e.entity_id); |
| 24 | + } |
| 25 | + |
| 26 | + async applyOrganizationWhereClause( |
| 27 | + where: Object | undefined, |
| 28 | + organizationId: number | undefined, |
| 29 | + model: string, |
| 30 | + ): Promise<Object | undefined> { |
| 31 | + if (!where || organizationId === undefined) { |
| 32 | + return Promise.resolve(where); |
| 33 | + } |
| 34 | + |
| 35 | + // if planter or tree repository request |
| 36 | + if (model === 'trees' || model === 'planter') { |
| 37 | + const organizationWhereClause = await this.getOrganizationWhereClause( |
| 38 | + organizationId, |
| 39 | + model, |
| 40 | + ); |
| 41 | + return { |
| 42 | + and: [where, organizationWhereClause], |
| 43 | + }; |
| 44 | + } else { |
| 45 | + const entityIds = await this.getEntityIdsByOrganizationId( |
| 46 | + organizationId, |
| 47 | + ); |
| 48 | + return { |
| 49 | + and: [where, { id: { inq: entityIds } }], |
| 50 | + }; |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + async getPlanterIdsByOrganizationId( |
| 55 | + organizationId: number, |
| 56 | + ): Promise<Array<number>> { |
| 57 | + expect(organizationId).number(); |
| 58 | + const result = await this.execute( |
| 59 | + `select * from planter where organization_id in (select entity_id from getEntityRelationshipChildren(${organizationId}))`, |
| 60 | + [], |
| 61 | + ); |
| 62 | + expect(result).match([{ id: expect.any(Number) }]); |
| 63 | + return result.map((e) => e.id); |
| 64 | + } |
| 65 | + |
| 66 | + async getNonOrganizationPlanterIds(): Promise<Array<number>> { |
| 67 | + const result = await this.execute( |
| 68 | + `select * from planter where organization_id isnull`, |
| 69 | + [], |
| 70 | + ); |
| 71 | + expect(result).match([{ id: expect.any(Number) }]); |
| 72 | + return result.map((e) => e.id); |
| 73 | + } |
| 74 | + |
| 75 | + async getOrganizationWhereClause( |
| 76 | + organizationId: number, |
| 77 | + model: string, |
| 78 | + ): Promise<Object> { |
| 79 | + if (organizationId === null) { |
| 80 | + const planterIds = await this.getNonOrganizationPlanterIds(); |
| 81 | + // if planter repository request |
| 82 | + if (model === 'planter') { |
| 83 | + return { |
| 84 | + or: [{ organizationId: null }, { id: { inq: planterIds } }], |
| 85 | + }; |
| 86 | + } else { |
| 87 | + // if trees or other repository request |
| 88 | + return { |
| 89 | + or: [ |
| 90 | + { plantingOrganizationId: null }, |
| 91 | + { planterId: { inq: planterIds } }, |
| 92 | + ], |
| 93 | + }; |
| 94 | + } |
| 95 | + } else { |
| 96 | + const planterIds = await this.getPlanterIdsByOrganizationId( |
| 97 | + organizationId, |
| 98 | + ); |
| 99 | + const entityIds = await this.getEntityIdsByOrganizationId( |
| 100 | + organizationId, |
| 101 | + ); |
| 102 | + // if planter repository request |
| 103 | + if (model === 'planter') { |
| 104 | + return { |
| 105 | + or: [ |
| 106 | + { organizationId: { inq: entityIds } }, |
| 107 | + { id: { inq: planterIds } }, |
| 108 | + ], |
| 109 | + }; |
| 110 | + } else { |
| 111 | + // if trees or other repository request |
| 112 | + return { |
| 113 | + or: [ |
| 114 | + { plantingOrganizationId: { inq: entityIds } }, |
| 115 | + { planterId: { inq: planterIds } }, |
| 116 | + ], |
| 117 | + }; |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + }; |
| 122 | +} |
0 commit comments