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

Commit

Permalink
feat: public blid search
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrianAndersen committed Nov 6, 2024
1 parent ec0a61d commit 93d63f1
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/collections/customer-item/customer-item.collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
import { CustomerItemGenerateReportOperation } from "@/collections/customer-item/customer-item-generate-report.operation";
import { customerItemSchema } from "@/collections/customer-item/customer-item.schema";
import { CustomerItemPostHook } from "@/collections/customer-item/hooks/customer-item-post.hook";
import { PublicBlidLookupOperation } from "@/collections/customer-item/public-blid-lookup.operation";
import { itemSchema } from "@/collections/item/item.schema";
import { userDetailSchema } from "@/collections/user-detail/user-detail.schema";

Expand Down Expand Up @@ -42,6 +43,13 @@ export class CustomerItemCollection implements BlCollection {
permissions: ["admin", "super"],
},
},
{
name: "public-blid-lookup",
operation: new PublicBlidLookupOperation(),
restriction: {
permissions: ["customer", "employee", "manager", "admin", "super"],
},
},
],
restriction: {
permissions: ["employee", "manager", "admin", "super"],
Expand Down
85 changes: 85 additions & 0 deletions src/collections/customer-item/public-blid-lookup.operation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { BlapiResponse, BlError, CustomerItem } from "@boklisten/bl-model";

import { BlCollectionName } from "@/collections/bl-collection";
import { customerItemSchema } from "@/collections/customer-item/customer-item.schema";
import { Operation } from "@/operation/operation";
import { BlApiRequest } from "@/request/bl-api-request";
import { BlDocumentStorage } from "@/storage/blDocumentStorage";

export interface PublicBlidLookupSpec {
blid: string;
}

export function verifyPublicBlidLookupSpec(
publicBlidLookupSpec: unknown,
): publicBlidLookupSpec is PublicBlidLookupSpec {
const m = publicBlidLookupSpec as Record<string, unknown> | null | undefined;
return !!m && typeof m["blid"] === "string";
}

export class PublicBlidLookupOperation 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 publicBlidLookupSpec = blApiRequest.data;
if (!verifyPublicBlidLookupSpec(publicBlidLookupSpec)) {
throw new BlError(`Malformed PublicBlidLookupSpec`).code(701);
}

const customerItemInfo = await this._customerItemStorage.aggregate([
{
$match: {
returned: false,
buyout: false,
cancel: false,
buyback: false,
blid: publicBlidLookupSpec.blid,
},
},
{
$lookup: {
from: "branches",
localField: "handoutInfo.handoutById",
foreignField: "_id",
as: "branchInfo",
},
},
{
$lookup: {
from: "items",
localField: "item",
foreignField: "_id",
as: "itemInfo",
},
},
{
$lookup: {
from: "userdetails",
localField: "customer",
foreignField: "_id",
as: "customerInfo",
},
},
{
$project: {
handoutBranch: { $first: "$branchInfo.name" },
handoutTime: "$handoutInfo.time",
deadline: 1,
title: { $first: "$itemInfo.title" },
isbn: { $toString: { $first: "$itemInfo.info.isbn" } },
name: { $first: "$customerInfo.name" },
email: { $first: "$customerInfo.email" },
phone: { $first: "$customerInfo.phone" },
},
},
]);

return new BlapiResponse(customerItemInfo);
}
}

0 comments on commit 93d63f1

Please sign in to comment.