This repository was archived by the owner on Dec 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
feat(reports): create endpoint for generating customer item report xlsx files #528
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
153 changes: 153 additions & 0 deletions
153
src/collections/customer-item/customer-item-generate-report.operation.ts
This file contains hidden or 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,153 @@ | ||
import { BlapiResponse, BlError, CustomerItem } from "@boklisten/bl-model"; | ||
import { ObjectId } from "mongodb"; | ||
|
||
import { customerItemSchema } from "./customer-item.schema"; | ||
import { isBoolean, isNullish } from "../../helper/typescript-helpers"; | ||
import { Operation } from "../../operation/operation"; | ||
import { BlApiRequest } from "../../request/bl-api-request"; | ||
import { BlDocumentStorage } from "../../storage/blDocumentStorage"; | ||
import { BlCollectionName } from "../bl-collection"; | ||
|
||
export interface CustomerItemGenerateReportSpec { | ||
branchFilter?: string[]; | ||
createdAfter?: string; | ||
createdBefore?: string; | ||
returned: boolean; | ||
buyout: boolean; | ||
} | ||
|
||
export function verifyCustomerItemGenerateReportSpec( | ||
customerItemGenerateReportSpec: unknown, | ||
): customerItemGenerateReportSpec is CustomerItemGenerateReportSpec { | ||
const m = customerItemGenerateReportSpec as | ||
| Record<string, unknown> | ||
| null | ||
| undefined; | ||
return ( | ||
!!m && | ||
isBoolean(m["returned"]) && | ||
isBoolean(m["buyout"]) && | ||
(isNullish(m["branchFilter"]) || | ||
(Array.isArray(m["branchFilter"]) && | ||
m["branchFilter"].every((branchId) => ObjectId.isValid(branchId)))) && | ||
(isNullish(m["createdAfter"]) || | ||
(typeof m["createdAfter"] === "string" && | ||
!isNaN(new Date(m["createdAfter"]).getTime()))) && | ||
(isNullish(m["createdBefore"]) || | ||
(typeof m["createdBefore"] === "string" && | ||
!isNaN(new Date(m["createdBefore"]).getTime()))) | ||
); | ||
} | ||
|
||
export class CustomerItemGenerateReportOperation implements Operation { | ||
private readonly _customerItemStorage: BlDocumentStorage<CustomerItem>; | ||
|
||
constructor(customerItemStorage?: BlDocumentStorage<CustomerItem>) { | ||
this._customerItemStorage = | ||
customerItemStorage ?? | ||
new BlDocumentStorage(BlCollectionName.CustomerItems, customerItemSchema); | ||
} | ||
|
||
async run(blApiRequest: BlApiRequest): Promise<BlapiResponse> { | ||
const customerItemGenerateReportSpec = blApiRequest.data; | ||
if (!verifyCustomerItemGenerateReportSpec(customerItemGenerateReportSpec)) { | ||
throw new BlError(`Malformed CustomerItemGenerateReportSpec`).code(701); | ||
} | ||
const filterByHandoutBranchIfPresent = | ||
customerItemGenerateReportSpec.branchFilter | ||
? { | ||
"handoutInfo.handoutById": { | ||
$in: customerItemGenerateReportSpec.branchFilter.map( | ||
(id) => new ObjectId(id), | ||
), | ||
}, | ||
} | ||
: {}; | ||
|
||
const creationTimeLimiter: Record<string, Date> = {}; | ||
if (customerItemGenerateReportSpec.createdAfter) { | ||
creationTimeLimiter["$gte"] = new Date( | ||
customerItemGenerateReportSpec.createdAfter, | ||
); | ||
} | ||
if (customerItemGenerateReportSpec.createdBefore) { | ||
creationTimeLimiter["$lte"] = new Date( | ||
customerItemGenerateReportSpec.createdBefore, | ||
); | ||
} | ||
const creationTimeFilter = | ||
Object.keys(creationTimeLimiter).length > 0 | ||
? { creationTime: creationTimeLimiter } | ||
: {}; | ||
|
||
const reportData = await this._customerItemStorage.aggregate([ | ||
{ | ||
$match: { | ||
returned: customerItemGenerateReportSpec.returned, | ||
buyout: customerItemGenerateReportSpec.buyout, | ||
...filterByHandoutBranchIfPresent, | ||
...creationTimeFilter, | ||
}, | ||
}, | ||
{ | ||
$lookup: { | ||
from: "branches", | ||
localField: "handoutInfo.handoutById", | ||
foreignField: "_id", | ||
as: "branchInfo", | ||
}, | ||
}, | ||
{ | ||
$lookup: { | ||
from: "items", | ||
localField: "item", | ||
foreignField: "_id", | ||
as: "itemInfo", | ||
}, | ||
}, | ||
{ | ||
$addFields: { | ||
customer: { | ||
$toObjectId: "$customer", | ||
}, | ||
}, | ||
}, | ||
{ | ||
$lookup: { | ||
from: "userdetails", | ||
localField: "customer", | ||
foreignField: "_id", | ||
as: "customerInfo", | ||
}, | ||
}, | ||
{ | ||
$lookup: { | ||
from: "userdetails", | ||
localField: "handoutInfo.handoutEmployee", | ||
foreignField: "_id", | ||
as: "employeeInfo", | ||
}, | ||
}, | ||
{ | ||
$project: { | ||
handoutBranch: { $first: "$branchInfo.name" }, | ||
handoutTime: "$handoutInfo.time", | ||
handoutEmployee: { $first: "$employeeInfo.name" }, | ||
lastUpdated: 1, | ||
deadline: 1, | ||
blid: 1, | ||
title: { $first: "$itemInfo.title" }, | ||
isbn: { $toString: { $first: "$itemInfo.info.isbn" } }, | ||
name: { $first: "$customerInfo.name" }, | ||
email: { $first: "$customerInfo.email" }, | ||
phone: { $first: "$customerInfo.phone" }, | ||
dob: { $first: "$customerInfo.dob" }, | ||
comments: 1, | ||
pivot: "1", | ||
AdrianAndersen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}, | ||
}, | ||
]); | ||
|
||
return new BlapiResponse(reportData); | ||
} | ||
} |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.