Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/common-libs/plan/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const getNextTag = async (
): Promise<string> => {
const latestPlanTag = await models.planTag.find({
where: {
planId: createBrandedValue(planTag.planId),
planId: planTag.planId,
},
orderBy: { column: 'createdAt', order: 'desc' },
});
Expand Down Expand Up @@ -38,7 +38,7 @@ export const setPlanReportingPeriod = async (
) => {
const reportingPeriods = await models.planReportingPeriod.find({
where: {
planId: createBrandedValue(planTag.planId),
planId: planTag.planId,
},
orderBy: { column: 'periodNumber', order: 'asc' },
});
Expand All @@ -51,7 +51,7 @@ export const setPlanReportingPeriod = async (

const latestPlanVersion = await models.planVersion.findOne({
where: {
planId: createBrandedValue(planTag.planId),
planId: planTag.planId,
latestVersion: true,
},
});
Expand Down
8 changes: 4 additions & 4 deletions src/common-libs/plan/versioning.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,11 @@ const updateBaseAndVersionModelTags = async (
}
idSet.add(baseRow.id);
} else {
inactiveRows.push(createBrandedValue(baseRow.id));
inactiveRows.push(baseRow.id);
}
}

for (const [versionTagsString, rowIds] of activeRows.entries()) {
for (const [versionTagsString, rowIds] of activeRows) {
const versionTags =
versionTagsString === '' ? [] : versionTagsString.split(',');

Expand Down Expand Up @@ -231,7 +231,7 @@ const updateBaseAndVersionModelTags = async (
idSet.add(latestVersion.id);
}

for (const [versionTagsString, rowIds] of versionTagsMap.entries()) {
for (const [versionTagsString, rowIds] of versionTagsMap) {
const versionTags =
versionTagsString === '' ? [] : versionTagsString.split(',');

Expand Down Expand Up @@ -329,7 +329,7 @@ const updateBaseModelTags = async (
},
});
} else {
inactiveRows.push(createBrandedValue(baseRow.id));
inactiveRows.push(baseRow.id);
}
}

Expand Down
69 changes: 27 additions & 42 deletions src/domain-services/categories/category-service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { type Database } from '@unocha/hpc-api-core/src/db';
import type { CategoryId } from '@unocha/hpc-api-core/src/db/models/category';
import { type FlowId } from '@unocha/hpc-api-core/src/db/models/flow';
import {
Cond,
Expand All @@ -7,6 +8,7 @@ import {
} from '@unocha/hpc-api-core/src/db/util/conditions';
import { type InstanceOfModel } from '@unocha/hpc-api-core/src/db/util/types';
import { getOrCreate } from '@unocha/hpc-api-core/src/util';
import { createBrandedValue } from '@unocha/hpc-api-core/src/util/types';
import { Service } from 'typedi';
import { type ReportDetail } from '../report-details/graphql/types';
import { type Category } from './graphql/types';
Expand All @@ -25,29 +27,24 @@ export class CategoryService {
async getCategoriesForFlows(
flowWithVersion: Map<FlowId, number[]>,
models: Database
): Promise<Map<number, Map<number, Category[]>>> {
): Promise<Map<FlowId, Map<number, Category[]>>> {
// Group of flowIDs and its versions
// Structure:
// flowID: {
// versionID: [categories]
// }
const flowVersionCategoryMap = new Map<number, Map<number, Category[]>>();
const flowVersionCategoryMap = new Map<FlowId, Map<number, Category[]>>();

const flowIDs: FlowId[] = [];
for (const flowID of flowWithVersion.keys()) {
flowIDs.push(flowID);
}

const categoriesRef: CategoryRefInstance[] = await models.categoryRef.find({
const categoriesRef = await models.categoryRef.find({
where: {
objectID: {
[Op.IN]: flowIDs,
[Op.IN]: flowWithVersion.keys(),
},
objectType: 'flow',
},
});

const categories: CategoryInstance[] = await models.category.find({
const categories = await models.category.find({
where: {
id: {
[Op.IN]: categoriesRef.map((catRef) => catRef.categoryID),
Expand All @@ -57,7 +54,7 @@ export class CategoryService {

// Populate the map with categories for each flow
for (const catRef of categoriesRef) {
const flowId = catRef.objectID.valueOf();
const flowId: FlowId = createBrandedValue(catRef.objectID);

if (!flowVersionCategoryMap.has(flowId)) {
flowVersionCategoryMap.set(flowId, new Map());
Expand All @@ -70,24 +67,17 @@ export class CategoryService {
() => new Map<number, Category[]>()
);

const flowVersion = catRef.versionID;
if (!flowVersionMap.has(flowVersion)) {
flowVersionMap.set(flowVersion, []);
}

const categoriesPerFlowVersion = getOrCreate(
flowVersionMap,
flowVersion,
catRef.versionID,
() => []
);

const category = categories.find((cat) => cat.id === catRef.categoryID);

if (
category &&
!categoriesPerFlowVersion.some(
(cat) => cat.id === category.id.valueOf()
)
!categoriesPerFlowVersion.some((cat) => cat.id === category.id)
) {
const mappedCategory = this.mapCategoryToFlowCategory(category, catRef);
categoriesPerFlowVersion.push(mappedCategory);
Expand All @@ -108,13 +98,13 @@ export class CategoryService {
createdAt: category.createdAt.toISOString(),
updatedAt: category.updatedAt.toISOString(),
description: category.description ?? '',
parentID: category.parentID ? category.parentID.valueOf() : null,
parentID: category.parentID,
code: category.code ?? '',
categoryRef: {
objectID: categoryRef.objectID.valueOf(),
objectID: categoryRef.objectID,
versionID: categoryRef.versionID,
objectType: categoryRef.objectType,
categoryID: category.id.valueOf(),
categoryID: category.id,
createdAt: categoryRef.createdAt.toISOString(),
updatedAt: categoryRef.updatedAt.toISOString(),
},
Expand All @@ -137,52 +127,47 @@ export class CategoryService {
listOfCategoryRefORs.push(orClause);
}

const categoriesRef: CategoryRefInstance[] = await models.categoryRef.find({
const categoriesRef = await models.categoryRef.find({
where: {
[Cond.OR]: listOfCategoryRefORs,
},
});

const mapOfCategoriesAndReportDetails = new Map<number, ReportDetail[]>();
const mapOfCategoriesAndReportDetails = new Map<
CategoryId,
ReportDetail[]
>();

for (const categoryRef of categoriesRef) {
const reportDetail = reportDetails.find(
(reportDetail) => reportDetail.id === categoryRef.objectID.valueOf()
(reportDetail) => reportDetail.id === categoryRef.objectID
);

if (!reportDetail) {
continue;
}

if (
!mapOfCategoriesAndReportDetails.has(categoryRef.categoryID.valueOf())
) {
mapOfCategoriesAndReportDetails.set(
categoryRef.categoryID.valueOf(),
[]
);
if (!mapOfCategoriesAndReportDetails.has(categoryRef.categoryID)) {
mapOfCategoriesAndReportDetails.set(categoryRef.categoryID, []);
}

const reportDetailsPerCategory = getOrCreate(
mapOfCategoriesAndReportDetails,
categoryRef.categoryID.valueOf(),
categoryRef.categoryID,
() => []
);
reportDetailsPerCategory.push(reportDetail);
}

const categories: CategoryInstance[] = await models.category.find({
const categories = await models.category.find({
where: {
id: {
[Op.IN]: categoriesRef.map((catRef) => catRef.categoryID),
},
},
});

for (const [
category,
reportDetails,
] of mapOfCategoriesAndReportDetails.entries()) {
for (const [category, reportDetails] of mapOfCategoriesAndReportDetails) {
const categoryObj = categories.find((cat) => cat.id === category);

if (!categoryObj) {
Expand Down Expand Up @@ -249,9 +234,9 @@ export class CategoryService {

const shortcutFilters: ShortcutCategoryFilter[] = usedFilters
.map((filter) => {
const categoryId = categories
.find((category) => category.name.includes(filter.category))
?.id.valueOf();
const categoryId = categories.find((category) =>
category.name.includes(filter.category)
)?.id;

return {
category: filter.category,
Expand Down
7 changes: 4 additions & 3 deletions src/domain-services/categories/graphql/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Field, Int, ObjectType } from 'type-graphql';
import type { CategoryId } from '@unocha/hpc-api-core/src/db/models/category';
import { Field, ID, Int, ObjectType } from 'type-graphql';
import { BaseType } from '../../base-types';

@ObjectType()
Expand All @@ -18,8 +19,8 @@ export class CategoryRef extends BaseType {

@ObjectType()
export class Category extends BaseType {
@Field({ nullable: true })
id: number;
@Field(() => ID, { nullable: true })
id: CategoryId | null;

@Field({ nullable: false })
name: string;
Expand Down
3 changes: 2 additions & 1 deletion src/domain-services/categories/model.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type { CategoryId } from '@unocha/hpc-api-core/src/db/models/category';
import { type Op } from '@unocha/hpc-api-core/src/db/util/conditions';

export type ShortcutCategoryFilter = {
category: string;
operation: typeof Op.IN | typeof Op.NOT_IN;
id?: number;
id?: CategoryId;
};
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ import { type FlowId } from '@unocha/hpc-api-core/src/db/models/flow';
import { Op } from '@unocha/hpc-api-core/src/db/util/conditions';
import { type InstanceDataOfModel } from '@unocha/hpc-api-core/src/db/util/raw-model';
import { type InstanceOfModel } from '@unocha/hpc-api-core/src/db/util/types';
import { createBrandedValue } from '@unocha/hpc-api-core/src/util/types';
import { Service } from 'typedi';
import { type FlowExternalReference } from '../flows/graphql/types';
import { type UniqueFlowEntity } from '../flows/model';
import { type SystemID } from '../report-details/graphql/types';

@Service()
Expand All @@ -21,7 +19,7 @@ export class ExternalReferenceService {
skipValidation: true,
});

const externalReferencesMap = new Map<number, FlowExternalReference[]>();
const externalReferencesMap = new Map<FlowId, FlowExternalReference[]>();

// First we add all flowIDs to the map
// Since there might be flows without external references
Expand Down Expand Up @@ -51,7 +49,7 @@ export class ExternalReferenceService {
async getUniqueFlowIDsBySystemID(
models: Database,
systemID: SystemID
): Promise<UniqueFlowEntity[]> {
): Promise<Set<string>> {
const externalRefences: Array<
InstanceDataOfModel<Database['externalReference']>
> = await models.externalReference.find({
Expand All @@ -61,13 +59,11 @@ export class ExternalReferenceService {
skipValidation: true,
});

const flowIDs: UniqueFlowEntity[] = [];

for (const reference of externalRefences) {
flowIDs.push(this.mapExternalDataToUniqueFlowEntity(reference));
}

return flowIDs;
return new Set(
externalRefences.map((externalReference) => {
return `${externalReference.flowID}:${externalReference.versionID}`;
})
);
}

private mapExternalReferenceToExternalReferenceFlows(
Expand All @@ -83,13 +79,4 @@ export class ExternalReferenceService {
versionID: externalReference.versionID ?? 0,
};
}

private mapExternalDataToUniqueFlowEntity(
external: InstanceDataOfModel<Database['externalReference']>
): UniqueFlowEntity {
return {
id: createBrandedValue(external.flowID),
versionID: external.versionID,
};
}
}
2 changes: 1 addition & 1 deletion src/domain-services/flow-link/flow-link-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class FlowLinkService {
async getFlowLinksForFlows(
flowIds: FlowId[],
models: Database
): Promise<Map<number, Array<InstanceOfModel<Database['flowLink']>>>> {
): Promise<Map<FlowId, Array<InstanceOfModel<Database['flowLink']>>>> {
// Fetch all flow links in one go
const flowLinks = await models.flowLink.find({
where: {
Expand Down
6 changes: 3 additions & 3 deletions src/domain-services/flow-object/flow-object-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import type {
FieldsOfModel,
InstanceOfModel,
} from '@unocha/hpc-api-core/src/db/util/types';
import { createBrandedValue } from '@unocha/hpc-api-core/src/util/types';
import { Service } from 'typedi';
import { type UniqueFlowEntity } from '../flows/model';
import { buildSearchFlowsObjectConditions } from '../flows/strategy/impl/utils';
Expand All @@ -22,6 +21,7 @@ type FlowObjectInstance = InstanceOfModel<FlowObjectModel>;
export type FlowObjectsFieldsDefinition = FieldsOfModel<FlowObjectModel>;
export type FlowObjectOrderByCond = OrderByCond<FlowObjectsFieldsDefinition>;
export type FlowObjectWhere = Condition<FlowObjectInstance>;

@Service()
export class FlowObjectService {
// Merge with getFlowsObjectsByFlows
Expand All @@ -48,7 +48,7 @@ export class FlowObjectService {
...new Set(
flowObjects.map((flowObject) => {
return {
id: createBrandedValue(flowObject.flowID),
id: flowObject.flowID,
versionID: flowObject.versionID,
};
})
Expand Down Expand Up @@ -92,7 +92,7 @@ export class FlowObjectService {
distinctColumns.reverse();
}

const flowsObjects: FlowObjectInstance[] = await models.flowObject.find({
const flowsObjects = await models.flowObject.find({
orderBy,
where: whereClauses,
distinct: distinctColumns,
Expand Down
6 changes: 3 additions & 3 deletions src/domain-services/flow-object/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ import { type FlowObjectFilterGrouped } from './model';
/**
* This alg iterates over the flowObjectFilters and creates a join for each flowObjectType
* and refDirection allowing to filter the flowObjects by the flowObjectType and refDirection
* inclusivelly for each
* inclusively for each
* @param flowObjectFiltersGrouped
* @returns FlowObjectWhere
*/
export function buildWhereConditionsForFlowObjectFilters(
flowObjectFiltersGrouped: FlowObjectFilterGrouped
): FlowObjectWhere {
const ANDConditions = [];
for (const [flowObjectType, group] of flowObjectFiltersGrouped.entries()) {
for (const [direction, ids] of group.entries()) {
for (const [flowObjectType, group] of flowObjectFiltersGrouped) {
for (const [direction, ids] of group) {
const condition = {
[Cond.AND]: [
{
Expand Down
Loading