Skip to content

Commit d81d15c

Browse files
Pl217manelcecs
authored andcommitted
Add Strong types
1 parent c311d17 commit d81d15c

File tree

3 files changed

+36
-27
lines changed

3 files changed

+36
-27
lines changed

src/domain-services/flows/flow-service.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ export class FlowService {
9696
const refDirection = orderBy.direction ?? 'source';
9797

9898
let entityIDsSorted: number[] = [];
99-
99+
let entityCondKeyFlowObjectType: FlowObjectType;
100100
switch (entity) {
101101
case 'emergency': {
102102
columns = getTableColumns(database.emergency);
@@ -119,6 +119,7 @@ export class FlowService {
119119
});
120120

121121
entityIDsSorted = emergencies.map((emergency) => emergency.id);
122+
entityCondKeyFlowObjectType = 'emergency' satisfies FlowObjectType;
122123
break;
123124
}
124125
case 'globalCluster': {
@@ -143,6 +144,8 @@ export class FlowService {
143144
entityIDsSorted = globalClusters.map(
144145
(globalCluster) => globalCluster.id
145146
);
147+
entityCondKeyFlowObjectType = 'globalCluster' satisfies FlowObjectType;
148+
146149
break;
147150
}
148151
case 'governingEntity': {
@@ -167,6 +170,9 @@ export class FlowService {
167170
entityIDsSorted = governingEntities.map(
168171
(governingEntity) => governingEntity.id
169172
);
173+
entityCondKeyFlowObjectType =
174+
'governingEntity' satisfies FlowObjectType;
175+
170176
break;
171177
}
172178
case 'location': {
@@ -189,6 +195,8 @@ export class FlowService {
189195
});
190196

191197
entityIDsSorted = locations.map((location) => location.id);
198+
entityCondKeyFlowObjectType = 'location' satisfies FlowObjectType;
199+
192200
break;
193201
}
194202
case 'organization': {
@@ -211,6 +219,8 @@ export class FlowService {
211219
});
212220

213221
entityIDsSorted = organizations.map((organization) => organization.id);
222+
entityCondKeyFlowObjectType = 'organization' satisfies FlowObjectType;
223+
214224
break;
215225
}
216226
case 'plan': {
@@ -233,6 +243,8 @@ export class FlowService {
233243
});
234244

235245
entityIDsSorted = plans.map((plan) => plan.id);
246+
entityCondKeyFlowObjectType = 'plan' satisfies FlowObjectType;
247+
236248
break;
237249
}
238250
case 'project': {
@@ -255,6 +267,8 @@ export class FlowService {
255267
});
256268

257269
entityIDsSorted = projects.map((project) => project.id);
270+
entityCondKeyFlowObjectType = 'project' satisfies FlowObjectType;
271+
258272
break;
259273
}
260274
case 'usageYear': {
@@ -277,6 +291,8 @@ export class FlowService {
277291
});
278292

279293
entityIDsSorted = usageYears.map((usageYear) => usageYear.id);
294+
entityCondKeyFlowObjectType = 'usageYear' satisfies FlowObjectType;
295+
280296
break;
281297
}
282298
case 'planVersion': {
@@ -304,6 +320,8 @@ export class FlowService {
304320
});
305321

306322
entityIDsSorted = planVersions.map((planVersion) => planVersion.planId);
323+
entityCondKeyFlowObjectType = 'plan' satisfies FlowObjectType;
324+
307325
break;
308326
}
309327
default: {
@@ -313,8 +331,6 @@ export class FlowService {
313331

314332
// After getting the sorted entityID list
315333
// we can now get the flowObjects
316-
const entityCondKey = orderBy.entity as unknown;
317-
const entityCondKeyFlowObjectType = entityCondKey as FlowObjectType;
318334

319335
// Order map
320336
const orderMap = new Map<number, number>();

src/domain-services/flows/graphql/types.ts

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { Organization } from '../../organizations/graphql/types';
88
import { BasePlan } from '../../plans/graphql/types';
99
import { ReportDetail } from '../../report-details/graphql/types';
1010
import { UsageYear } from '../../usage-years/graphql/types';
11+
import { FlowKeys } from '../model';
1112

1213
@ObjectType()
1314
export class FlowExternalReference {
@@ -176,26 +177,6 @@ export class FlowSearchResultNonPaginated {
176177
flowsCount: number;
177178
}
178179

179-
export type FlowSortField =
180-
| 'flow.id'
181-
| 'flow.versionID'
182-
| 'flow.amountUSD'
183-
| 'flow.updatedAt'
184-
| 'flow.activeStatus'
185-
| 'flow.restricted'
186-
| 'flow.newMoney'
187-
| 'flow.flowDate'
188-
| 'flow.decisionDate'
189-
| 'flow.firstReportedDate'
190-
| 'flow.budgetYear'
191-
| 'flow.origAmount'
192-
| 'flow.origCurrency'
193-
| 'flow.exchangeRate'
194-
| 'flow.description'
195-
| 'flow.notes'
196-
| 'flow.versionStartDate'
197-
| 'flow.versionEndDate'
198-
| 'flow.createdAt'
199-
| 'flow.deletedAt';
180+
export type FlowSortField = `flow.${FlowKeys}`
200181

201182
export type FlowStatusFilter = 'new' | 'updated' | undefined;

src/domain-services/flows/model.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,34 @@ import type {
88
} from '@unocha/hpc-api-core/src/db/util/types';
99
import { type SortOrder } from '../../utils/graphql/pagination';
1010
import { type EntityDirection } from '../base-types';
11+
import { type FlowObjectType } from '../flow-object/model';
1112

1213
export type FlowModel = Database['flow'];
1314
export type FlowInstance = InstanceOfModel<FlowModel>;
1415
export type FlowWhere = Condition<FlowInstance>;
1516
export type FlowFieldsDefinition = FieldsOfModel<FlowModel>;
17+
export type FlowKeys = Extract<keyof FlowInstance, string>;
1618
export type FlowOrderByCond = OrderByCond<FlowFieldsDefinition>; // Can this be simplified somehow?
1719
export type UniqueFlowEntity = {
1820
id: FlowId;
1921
versionID: number | null;
2022
};
2123

2224
export type FlowOrderByWithSubEntity = {
23-
column: keyof FlowInstance | string;
25+
column:
26+
| keyof FlowInstance
27+
| keyof InstanceOfModel<Database['emergency']>
28+
| keyof InstanceOfModel<Database['globalCluster']>
29+
| keyof InstanceOfModel<Database['governingEntity']>
30+
| keyof InstanceOfModel<Database['location']>
31+
| keyof InstanceOfModel<Database['plan']>
32+
| keyof InstanceOfModel<Database['planVersion']>
33+
| keyof InstanceOfModel<Database['project']>
34+
| keyof InstanceOfModel<Database['organization']>
35+
| keyof InstanceOfModel<Database['usageYear']>;
2436
order: SortOrder;
25-
entity: string;
26-
subEntity?: string;
37+
entity: 'flow' | 'externalReference';
38+
subEntity?: FlowObjectType | 'planVersion';
2739
direction?: EntityDirection;
2840
};
2941

0 commit comments

Comments
 (0)