Skip to content
This repository was archived by the owner on Dec 18, 2024. It is now read-only.

feat(reports): create endpoint for generating customer item report xlsx files #528

Merged
merged 3 commits into from
Jul 2, 2024
Merged
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
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",
},
},
]);

return new BlapiResponse(reportData);
}
}
10 changes: 10 additions & 0 deletions src/collections/customer-item/customer-item.collection.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { CustomerItemGenerateReportOperation } from "./customer-item-generate-report.operation";
import { customerItemSchema } from "./customer-item.schema";
import { CustomerItemPostHook } from "./hooks/customer-item-post.hook";
import { itemSchema } from "../../collections/item/item.schema";
Expand Down Expand Up @@ -33,6 +34,15 @@ export class CustomerItemCollection implements BlCollection {
{
method: "post",
hook: new CustomerItemPostHook(),
operations: [
{
name: "generate-report",
operation: new CustomerItemGenerateReportOperation(),
restriction: {
permissions: ["admin", "super"],
},
},
],
restriction: {
permissions: ["employee", "manager", "admin", "super"],
},
Expand Down
Loading