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.
feat(guardian-signature): implement public hook for guardian signatur…
…es (#538) * feat(guardian-signature): implement public hook for guardian signatures * feat(guardian-signature): add endpoint for checking guardian signature status * refactor(signature): improve func name * fix(guardian-signature): send email request for signature and fix types * fix: lint and old test * refactor(signature): slight naming * feat(guardian-signature): use SendGrid email templates * fix(guardian-signature): remove old email sendout * fix(guardian-signature): simplify type check * fix(guardian-signature): even more simplifed types
- Loading branch information
1 parent
82e09e2
commit 0383e6c
Showing
14 changed files
with
303 additions
and
167 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
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
81 changes: 81 additions & 0 deletions
81
src/collections/signature/operations/check-guardian-signature.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,81 @@ | ||
import { BlapiResponse, BlError, UserDetail } from "@boklisten/bl-model"; | ||
import { CheckGuardianSignatureSpec } from "@boklisten/bl-model/signature/serialized-signature"; | ||
import { ObjectId } from "mongodb"; | ||
|
||
import { Operation } from "../../../operation/operation"; | ||
import { BlApiRequest } from "../../../request/bl-api-request"; | ||
import { BlDocumentStorage } from "../../../storage/blDocumentStorage"; | ||
import { BlCollectionName } from "../../bl-collection"; | ||
import { userDetailSchema } from "../../user-detail/user-detail.schema"; | ||
import { | ||
getValidUserSignature, | ||
isGuardianSignatureRequired, | ||
isUnderage, | ||
} from "../helpers/signature.helper"; | ||
import { Signature, signatureSchema } from "../signature.schema"; | ||
|
||
export class CheckGuardianSignatureOperation implements Operation { | ||
private readonly _userDetailStorage: BlDocumentStorage<UserDetail>; | ||
private readonly _signatureStorage: BlDocumentStorage<Signature>; | ||
|
||
constructor( | ||
signatureStorage?: BlDocumentStorage<Signature>, | ||
userDetailStorage?: BlDocumentStorage<UserDetail>, | ||
) { | ||
this._signatureStorage = | ||
signatureStorage ?? | ||
new BlDocumentStorage(BlCollectionName.Signatures, signatureSchema); | ||
this._userDetailStorage = | ||
userDetailStorage ?? | ||
new BlDocumentStorage(BlCollectionName.UserDetails, userDetailSchema); | ||
} | ||
|
||
async run(blApiRequest: BlApiRequest): Promise<BlapiResponse> { | ||
const serializedGuardianSignature = blApiRequest.data; | ||
if (!validateSerializedGuardianSignature(serializedGuardianSignature)) | ||
throw new BlError("Bad serialized guardian signature").code(701); | ||
|
||
const userDetail = await this._userDetailStorage.get( | ||
serializedGuardianSignature.customerId, | ||
); | ||
|
||
if (!isUnderage(userDetail)) { | ||
return new BlapiResponse([ | ||
{ | ||
message: `${userDetail.name} er myndig, og trenger derfor ikke signatur fra foreldre.`, | ||
guardianSignatureRequired: false, | ||
}, | ||
]); | ||
} | ||
|
||
if ( | ||
!(await isGuardianSignatureRequired(userDetail, this._signatureStorage)) | ||
) { | ||
const signature = await getValidUserSignature( | ||
userDetail, | ||
this._signatureStorage, | ||
); | ||
return new BlapiResponse([ | ||
{ | ||
message: `${signature?.signingName} har allerede signert på vegne av ${userDetail.name}`, | ||
guardianSignatureRequired: false, | ||
}, | ||
]); | ||
} | ||
|
||
return new BlapiResponse([ | ||
{ customerName: userDetail.name, guardianSignatureRequired: true }, | ||
]); | ||
} | ||
} | ||
|
||
function validateSerializedGuardianSignature( | ||
serializedGuardianSignature: unknown, | ||
): serializedGuardianSignature is CheckGuardianSignatureSpec { | ||
const s = serializedGuardianSignature as Partial<CheckGuardianSignatureSpec>; | ||
return ( | ||
s != null && | ||
typeof s.customerId === "string" && | ||
ObjectId.isValid(s.customerId) | ||
); | ||
} |
96 changes: 96 additions & 0 deletions
96
src/collections/signature/operations/guardian-signature.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,96 @@ | ||
import { BlapiResponse, BlError, Order, UserDetail } from "@boklisten/bl-model"; | ||
import { SerializedGuardianSignature } from "@boklisten/bl-model/signature/serialized-signature"; | ||
import { ObjectId } from "mongodb"; | ||
|
||
import { SystemUser } from "../../../auth/permission/permission.service"; | ||
import { Operation } from "../../../operation/operation"; | ||
import { BlApiRequest } from "../../../request/bl-api-request"; | ||
import { BlDocumentStorage } from "../../../storage/blDocumentStorage"; | ||
import { BlCollectionName } from "../../bl-collection"; | ||
import { orderSchema } from "../../order/order.schema"; | ||
import { userDetailSchema } from "../../user-detail/user-detail.schema"; | ||
import { | ||
deserializeBase64EncodedImage, | ||
isGuardianSignatureRequired, | ||
serializeSignature, | ||
signOrders, | ||
} from "../helpers/signature.helper"; | ||
import { Signature, signatureSchema } from "../signature.schema"; | ||
|
||
export class GuardianSignatureOperation implements Operation { | ||
private readonly _userDetailStorage: BlDocumentStorage<UserDetail>; | ||
private readonly _orderStorage: BlDocumentStorage<Order>; | ||
private readonly _signatureStorage: BlDocumentStorage<Signature>; | ||
|
||
constructor( | ||
signatureStorage?: BlDocumentStorage<Signature>, | ||
orderStorage?: BlDocumentStorage<Order>, | ||
userDetailStorage?: BlDocumentStorage<UserDetail>, | ||
) { | ||
this._signatureStorage = | ||
signatureStorage ?? | ||
new BlDocumentStorage(BlCollectionName.Signatures, signatureSchema); | ||
this._orderStorage = | ||
orderStorage ?? | ||
new BlDocumentStorage(BlCollectionName.Orders, orderSchema); | ||
this._userDetailStorage = | ||
userDetailStorage ?? | ||
new BlDocumentStorage(BlCollectionName.UserDetails, userDetailSchema); | ||
} | ||
|
||
async run(blApiRequest: BlApiRequest): Promise<BlapiResponse> { | ||
const serializedGuardianSignature = blApiRequest.data; | ||
if (!validateSerializedGuardianSignature(serializedGuardianSignature)) | ||
throw new BlError("Bad serialized guardian signature").code(701); | ||
|
||
const userDetail = await this._userDetailStorage.get( | ||
serializedGuardianSignature.customerId, | ||
); | ||
|
||
if ( | ||
!(await isGuardianSignatureRequired(userDetail, this._signatureStorage)) | ||
) { | ||
throw new BlError( | ||
"Valid guardian signature is already present or not needed.", | ||
).code(813); | ||
} | ||
|
||
const signatureImage = await deserializeBase64EncodedImage( | ||
serializedGuardianSignature.base64EncodedImage, | ||
); | ||
|
||
const writtenSignature = await this._signatureStorage.add( | ||
{ | ||
// @ts-expect-error id will be auto-generated | ||
id: null, | ||
image: signatureImage, | ||
signedByGuardian: true, | ||
signingName: serializedGuardianSignature.signingName, | ||
}, | ||
new SystemUser(), | ||
); | ||
|
||
await this._userDetailStorage.update( | ||
userDetail.id, | ||
{ signatures: [...userDetail.signatures, writtenSignature.id] }, | ||
new SystemUser(), | ||
); | ||
|
||
await signOrders(this._orderStorage, userDetail); | ||
|
||
return new BlapiResponse([serializeSignature(writtenSignature)]); | ||
} | ||
} | ||
|
||
function validateSerializedGuardianSignature( | ||
serializedGuardianSignature: unknown, | ||
): serializedGuardianSignature is SerializedGuardianSignature { | ||
const s = serializedGuardianSignature as Partial<SerializedGuardianSignature>; | ||
return ( | ||
s != null && | ||
typeof s.customerId === "string" && | ||
ObjectId.isValid(s.customerId) && | ||
typeof s.base64EncodedImage === "string" && | ||
typeof s.signingName === "string" | ||
); | ||
} |
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
Oops, something went wrong.