This repository has been 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
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ec0a61d
commit 93d63f1
Showing
2 changed files
with
93 additions
and
0 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
85 changes: 85 additions & 0 deletions
85
src/collections/customer-item/public-blid-lookup.operation.ts
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,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); | ||
} | ||
} |