Skip to content

Commit

Permalink
feat: digital signatures (#228)
Browse files Browse the repository at this point in the history
* refactor: move editable-text to proper folder

* feat: add digital signatures

* v0.19.14

* fix: export SignatureService

* v0.19.15

* fix: export SignatureService from public_api

* v0.19.16

* feat: add signature helpers

* v0.19.17

* feat(signature): take now-timestamp

* v0.19.18

* refactor(signature): don't require UserDetailService

* v0.19.19

* feat(branch): add upper secondary check method

* 0.19.20

* Revert "feat(branch): add upper secondary check method"

This reverts commit fbf473f.

* chore: prettier

* build(deps): bump prettier

* 0.19.21
  • Loading branch information
LarsSelbekk authored May 15, 2024
1 parent 4714923 commit d2bf588
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 15 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@boklisten/bl-connect",
"version": "0.19.13",
"version": "0.19.21",
"license": "MIT",
"scripts": {
"dev": "yarn serve",
Expand Down Expand Up @@ -34,7 +34,7 @@
"@angular/platform-browser": "^11.1.2",
"@angular/platform-browser-dynamic": "^11.1.2",
"@auth0/angular-jwt": "^5.0.2",
"@boklisten/bl-model": "^0.25.30",
"@boklisten/bl-model": "^0.25.37",
"@types/jasmine": "~3.6.3",
"@types/jasminewd2": "~2.0.8",
"@types/node": "^14.14.25",
Expand Down
3 changes: 2 additions & 1 deletion public_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,5 @@ export * from "./src/app/document-services/match/match.service";

export * from "./src/app/document-services/unique-item/unique-item.service";

export * from "./src/app/editable-text/editable-text.service";
export * from "./src/app/document-services/editable-text/editable-text.service";
export * from "./src/app/document-services/signature/signature.service";
1 change: 1 addition & 0 deletions src/app/bl-connect/bl-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export const BL_CONFIG = {
booking: "bookings",
uniqueItem: "uniqueitems",
editableTexts: "editabletexts",
signature: "signatures",
},
editableTextIds: {
newsBanner: "65a7f68e81488330ddcd6fd3",
Expand Down
4 changes: 3 additions & 1 deletion src/app/bl-connect/bl-connect.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { BranchService } from "../document-services/branch/branch.service";
import { OpeningHourService } from "../document-services/opening-hour/opening-hour.service";
import { ApiErrorService } from "../api-error/api-error.service";
import { UserDetailService } from "../document-services/user-detail/user-detail.service";
import { EditableTextService } from "../editable-text/editable-text.service";
import { EditableTextService } from "../document-services/editable-text/editable-text.service";
import { TokenService } from "../token/token.service";
import { StorageService } from "../storage/storage.service";
import { LoginService } from "../login/login.service";
Expand Down Expand Up @@ -35,6 +35,7 @@ import { CompanyService } from "../document-services/company/company.service";
import { MatchService } from "../document-services/match/match.service";
import { BookingService } from "../document-services/booking/booking.service";
import { UniqueItemService } from "../document-services/unique-item/unique-item.service";
import { SignatureService } from "../document-services/signature/signature.service";

export function tokenGetter() {
return localStorage.getItem(BL_CONFIG.token.accessToken);
Expand Down Expand Up @@ -95,6 +96,7 @@ export function tokenGetter() {
BookingService,
UniqueItemService,
EditableTextService,
SignatureService,
],
})
export class BlConnectModule {}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Injectable } from "@angular/core";
import { BlApiError, EditableText } from "@boklisten/bl-model";
import { ApiService } from "../api/api.service";
import { BL_CONFIG } from "../bl-connect/bl-config";
import { ApiService } from "../../api/api.service";
import { BL_CONFIG } from "../../bl-connect/bl-config";

@Injectable()
export class EditableTextService {
Expand Down
59 changes: 59 additions & 0 deletions src/app/document-services/signature/signature.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { Injectable } from "@angular/core";
import {
SerializedSignature,
SignatureMetadata,
UserDetail,
} from "@boklisten/bl-model";
import { UserDetailService } from "../../document-services/user-detail/user-detail.service";
import { BL_CONFIG } from "../../bl-connect/bl-config";
import { BlDocumentService } from "../../document/bl-document.service";
import { CachedDocumentService } from "../../document/cached-document.service";

@Injectable()
export class SignatureService extends BlDocumentService<SerializedSignature> {
constructor(
private cachedDocumentService: CachedDocumentService,
private _userDetailService: UserDetailService
) {
super(cachedDocumentService);
this.setCollection(BL_CONFIG.collection.signature);
}

public base64EncodeImage(signatureImage: Buffer): string {
return signatureImage.toString("base64");
}

public async hasValidSignature(
userDetail: UserDetail,
now: Date
): Promise<boolean> {
if (userDetail.signatures.length === 0) {
return false;
}

const latestSignature = await this.getById(userDetail.signatures[0]);
if (this.isSignatureExpired(latestSignature, now)) {
return false;
}

return (
this._userDetailService.isUnderage(userDetail, now) ===
latestSignature.signedByGuardian
);
}

public isSignatureExpired(
signature: SignatureMetadata,
now: Date
): boolean {
if (!signature.creationTime) {
return true;
}
const oldestAllowedSignatureTime = new Date(
now.getFullYear(),
now.getMonth() - SignatureMetadata.NUM_MONTHS_VALID,
now.getDate()
);
return signature.creationTime < oldestAllowedSignatureTime;
}
}
16 changes: 11 additions & 5 deletions src/app/document-services/user-detail/user-detail.service.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
import { Injectable } from "@angular/core";
import { BlApiError, UserDetail } from "@boklisten/bl-model";
import { ApiService } from "../../api/api.service";
import { BL_CONFIG } from "../../bl-connect/bl-config";
import {
CachedDocumentService,
CachedDocumentServiceOptions,
} from "../../document/cached-document.service";
import { BlDocumentService } from "../../document/bl-document.service";
import { CachedDocumentService } from "../../document/cached-document.service";

@Injectable()
export class UserDetailService extends BlDocumentService<UserDetail> {
Expand All @@ -32,4 +28,14 @@ export class UserDetailService extends BlDocumentService<UserDetail> {
throw blApiError;
});
}

public isUnderage(userDetail: UserDetail, now: Date): boolean {
const latestAdultBirthDate = new Date(
now.getFullYear() - 18,
now.getMonth(),
now.getDate()
);

return userDetail.dob > latestAdultBirthDate;
}
}
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1214,10 +1214,10 @@
"@babel/helper-validator-identifier" "^7.22.20"
to-fast-properties "^2.0.0"

"@boklisten/bl-model@^0.25.30":
version "0.25.30"
resolved "https://registry.yarnpkg.com/@boklisten/bl-model/-/bl-model-0.25.30.tgz#8989f5fba19d8aa5486d8488d5be7e12f3778083"
integrity sha512-rh250euu/zLldrFNxB9rfTATQ0e3eS6wAFiMJGQ2AUSJTkOXUe9+ozm1/Ft6xkx7Pn7LWsmnLHzuadHLYA/g0A==
"@boklisten/bl-model@^0.25.37":
version "0.25.37"
resolved "https://registry.yarnpkg.com/@boklisten/bl-model/-/bl-model-0.25.37.tgz#53cf9ea5b38bc953ada24b3d1c353707d83496fc"
integrity sha512-+KgdcMqD390D9HCQ/3x5uRpSJdSdbj1s/1mjEbmo6oy48IDwnS9qJbzUFXZ4V0G+4QmrG+cE6AJ3MCC8UNiiBA==
dependencies:
typescript "^5.2.2"

Expand Down

0 comments on commit d2bf588

Please sign in to comment.