-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #306 from UN-OCHA/env/stage
🚀 Release `v4.8.0` and deploy to production
- Loading branch information
Showing
43 changed files
with
4,764 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,23 @@ | ||
import { Field, ObjectType } from 'type-graphql'; | ||
|
||
export type EntityDirection = 'source' | 'destination'; | ||
@ObjectType() | ||
export class BaseType { | ||
@Field() | ||
createdAt: Date; | ||
createdAt: string; | ||
|
||
@Field() | ||
updatedAt: Date; | ||
updatedAt: string; | ||
} | ||
|
||
@ObjectType() | ||
export class BaseTypeWithDirection extends BaseType { | ||
@Field() | ||
direction: EntityDirection; | ||
} | ||
|
||
@ObjectType() | ||
export class BaseTypeWithSoftDelete extends BaseType { | ||
@Field({ nullable: true }) | ||
deletedAt: Date; | ||
@Field(() => String, { nullable: true }) | ||
deletedAt: string | null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,267 @@ | ||
import { type Database } from '@unocha/hpc-api-core/src/db'; | ||
import { type FlowId } from '@unocha/hpc-api-core/src/db/models/flow'; | ||
import { | ||
Cond, | ||
Op, | ||
type Condition, | ||
} 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 { Service } from 'typedi'; | ||
import { type ReportDetail } from '../report-details/graphql/types'; | ||
import { type Category } from './graphql/types'; | ||
import { type ShortcutCategoryFilter } from './model'; | ||
|
||
// Local types definition to increase readability | ||
type CategoryRefModel = Database['categoryRef']; | ||
type CategoryRefInstance = InstanceOfModel<CategoryRefModel>; | ||
|
||
type CategoryModel = Database['category']; | ||
type CategoryInstance = InstanceOfModel<CategoryModel>; | ||
type CategoryWhere = Condition<CategoryInstance>; | ||
|
||
@Service() | ||
export class CategoryService { | ||
async getCategoriesForFlows( | ||
flowWithVersion: Map<FlowId, number[]>, | ||
models: Database | ||
): Promise<Map<number, Map<number, Category[]>>> { | ||
// Group of flowIDs and its versions | ||
// Structure: | ||
// flowID: { | ||
// versionID: [categories] | ||
// } | ||
const flowVersionCategoryMap = new Map<number, Map<number, Category[]>>(); | ||
|
||
const flowIDs: FlowId[] = []; | ||
for (const flowID of flowWithVersion.keys()) { | ||
flowIDs.push(flowID); | ||
} | ||
|
||
const categoriesRef: CategoryRefInstance[] = await models.categoryRef.find({ | ||
where: { | ||
objectID: { | ||
[Op.IN]: flowIDs, | ||
}, | ||
objectType: 'flow', | ||
}, | ||
}); | ||
|
||
const categories: CategoryInstance[] = await models.category.find({ | ||
where: { | ||
id: { | ||
[Op.IN]: categoriesRef.map((catRef) => catRef.categoryID), | ||
}, | ||
}, | ||
}); | ||
|
||
// Populate the map with categories for each flow | ||
for (const catRef of categoriesRef) { | ||
const flowId = catRef.objectID.valueOf(); | ||
|
||
if (!flowVersionCategoryMap.has(flowId)) { | ||
flowVersionCategoryMap.set(flowId, new Map()); | ||
} | ||
|
||
// Here the key is the versionID of the flow | ||
const flowVersionMap = getOrCreate( | ||
flowVersionCategoryMap, | ||
flowId, | ||
() => new Map<number, Category[]>() | ||
); | ||
|
||
const flowVersion = catRef.versionID; | ||
if (!flowVersionMap.has(flowVersion)) { | ||
flowVersionMap.set(flowVersion, []); | ||
} | ||
|
||
const categoriesPerFlowVersion = getOrCreate( | ||
flowVersionMap, | ||
flowVersion, | ||
() => [] | ||
); | ||
|
||
const category = categories.find((cat) => cat.id === catRef.categoryID); | ||
|
||
if ( | ||
category && | ||
!categoriesPerFlowVersion.some( | ||
(cat) => cat.id === category.id.valueOf() | ||
) | ||
) { | ||
const mappedCategory = this.mapCategoryToFlowCategory(category, catRef); | ||
categoriesPerFlowVersion.push(mappedCategory); | ||
} | ||
} | ||
|
||
return flowVersionCategoryMap; | ||
} | ||
|
||
private mapCategoryToFlowCategory( | ||
category: CategoryInstance, | ||
categoryRef: CategoryRefInstance | ||
): Category { | ||
return { | ||
id: category.id, | ||
name: category.name, | ||
group: category.group, | ||
createdAt: category.createdAt.toISOString(), | ||
updatedAt: category.updatedAt.toISOString(), | ||
description: category.description ?? '', | ||
parentID: category.parentID ? category.parentID.valueOf() : null, | ||
code: category.code ?? '', | ||
includeTotals: category.includeTotals ?? false, | ||
categoryRef: { | ||
objectID: categoryRef.objectID.valueOf(), | ||
versionID: categoryRef.versionID, | ||
objectType: categoryRef.objectType, | ||
categoryID: category.id.valueOf(), | ||
createdAt: categoryRef.createdAt.toISOString(), | ||
updatedAt: categoryRef.updatedAt.toISOString(), | ||
}, | ||
versionID: categoryRef.versionID, | ||
}; | ||
} | ||
|
||
async addChannelToReportDetails( | ||
models: Database, | ||
reportDetails: ReportDetail[] | ||
): Promise<ReportDetail[]> { | ||
const listOfCategoryRefORs: Array<Condition<CategoryRefInstance>> = []; | ||
|
||
for (const reportDetail of reportDetails) { | ||
const orClause = { | ||
objectID: reportDetail.id, | ||
objectType: 'reportDetail', | ||
} satisfies Condition<CategoryRefInstance>; | ||
|
||
listOfCategoryRefORs.push(orClause); | ||
} | ||
|
||
const categoriesRef: CategoryRefInstance[] = await models.categoryRef.find({ | ||
where: { | ||
[Cond.OR]: listOfCategoryRefORs, | ||
}, | ||
}); | ||
|
||
const mapOfCategoriesAndReportDetails = new Map<number, ReportDetail[]>(); | ||
|
||
for (const categoryRef of categoriesRef) { | ||
const reportDetail = reportDetails.find( | ||
(reportDetail) => reportDetail.id === categoryRef.objectID.valueOf() | ||
); | ||
|
||
if (!reportDetail) { | ||
continue; | ||
} | ||
|
||
if ( | ||
!mapOfCategoriesAndReportDetails.has(categoryRef.categoryID.valueOf()) | ||
) { | ||
mapOfCategoriesAndReportDetails.set( | ||
categoryRef.categoryID.valueOf(), | ||
[] | ||
); | ||
} | ||
|
||
const reportDetailsPerCategory = getOrCreate( | ||
mapOfCategoriesAndReportDetails, | ||
categoryRef.categoryID.valueOf(), | ||
() => [] | ||
); | ||
reportDetailsPerCategory.push(reportDetail); | ||
} | ||
|
||
const categories: CategoryInstance[] = await models.category.find({ | ||
where: { | ||
id: { | ||
[Op.IN]: categoriesRef.map((catRef) => catRef.categoryID), | ||
}, | ||
}, | ||
}); | ||
|
||
for (const [ | ||
category, | ||
reportDetails, | ||
] of mapOfCategoriesAndReportDetails.entries()) { | ||
const categoryObj = categories.find((cat) => cat.id === category); | ||
|
||
if (!categoryObj) { | ||
continue; | ||
} | ||
|
||
for (const reportDetail of reportDetails) { | ||
reportDetail.channel = categoryObj.name; | ||
} | ||
} | ||
|
||
return reportDetails; | ||
} | ||
|
||
/** | ||
* This method returns the shortcut filter defined with the operation | ||
* IN if is true or NOT IN if is false | ||
* | ||
* @param isPendingFlows | ||
* @param isCommitmentFlows | ||
* @param isPaidFlows | ||
* @param isPledgedFlows | ||
* @param isCarryoverFlows | ||
* @param isParkedFlows | ||
* @param isPassThroughFlows | ||
* @param isStandardFlows | ||
* @returns [{ category: String, operation: Op.IN | Op.NOT_IN}] | ||
*/ | ||
async mapShortcutFilters( | ||
models: Database, | ||
isPendingFlows: boolean, | ||
isCommitmentFlows: boolean, | ||
isPaidFlows: boolean, | ||
isPledgedFlows: boolean, | ||
isCarryoverFlows: boolean, | ||
isParkedFlows: boolean, | ||
isPassThroughFlows: boolean, | ||
isStandardFlows: boolean | ||
): Promise<ShortcutCategoryFilter[] | null> { | ||
const filters = [ | ||
{ flag: isPendingFlows, category: 'Pending' }, | ||
{ flag: isCommitmentFlows, category: 'Commitment' }, | ||
{ flag: isPaidFlows, category: 'Paid' }, | ||
{ flag: isPledgedFlows, category: 'Pledge' }, | ||
{ flag: isCarryoverFlows, category: 'Carryover' }, | ||
{ flag: isParkedFlows, category: 'Parked' }, | ||
{ flag: isPassThroughFlows, category: 'Pass Through' }, | ||
{ flag: isStandardFlows, category: 'Standard' }, | ||
]; | ||
|
||
const usedFilters = filters.filter((filter) => filter.flag !== undefined); | ||
|
||
const searchCategories = usedFilters.map((filter) => filter.category); | ||
|
||
const whereClause: CategoryWhere = { | ||
[Cond.OR]: searchCategories.map((cat) => ({ | ||
name: { [Op.ILIKE]: `%${cat}%` }, | ||
})), | ||
}; | ||
|
||
const categories = await models.category.find({ | ||
where: whereClause, | ||
}); | ||
|
||
const shortcutFilters: ShortcutCategoryFilter[] = usedFilters | ||
.map((filter) => { | ||
const categoryId = categories | ||
.find((category) => category.name.includes(filter.category)) | ||
?.id.valueOf(); | ||
|
||
return { | ||
category: filter.category, | ||
operation: filter.flag ? Op.IN : Op.NOT_IN, | ||
id: categoryId, | ||
} satisfies ShortcutCategoryFilter; | ||
}) | ||
.filter((filter) => filter.id !== undefined); | ||
|
||
return shortcutFilters.length > 0 ? shortcutFilters : null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { Field, Int, ObjectType } from 'type-graphql'; | ||
import { BaseType } from '../../base-types'; | ||
|
||
@ObjectType() | ||
export class CategoryRef extends BaseType { | ||
@Field({ nullable: false }) | ||
objectID: number; | ||
|
||
@Field({ nullable: false }) | ||
versionID: number; | ||
|
||
@Field({ nullable: false }) | ||
objectType: string; | ||
|
||
@Field({ nullable: false }) | ||
categoryID: number; | ||
} | ||
|
||
@ObjectType() | ||
export class Category extends BaseType { | ||
@Field({ nullable: true }) | ||
id: number; | ||
|
||
@Field({ nullable: false }) | ||
name: string; | ||
|
||
@Field({ nullable: false }) | ||
group: string; | ||
|
||
@Field({ nullable: true }) | ||
description: string; | ||
|
||
@Field(() => Int, { nullable: true }) | ||
parentID: number | null; | ||
|
||
@Field({ nullable: true }) | ||
code: string; | ||
|
||
@Field({ nullable: true }) | ||
includeTotals: boolean; | ||
|
||
@Field(() => CategoryRef, { nullable: true }) | ||
categoryRef: CategoryRef; | ||
|
||
@Field({ nullable: false }) | ||
versionID: number; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
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; | ||
}; |
Oops, something went wrong.