From 38c48fee2ab30041bf65f18139e0b99bb9b17b8a Mon Sep 17 00:00:00 2001 From: tenretC Date: Tue, 10 Apr 2018 11:48:25 +0200 Subject: [PATCH] feat(user): add entities for user module --- .../stark-core/src/user/entities/index.ts | 5 ++ .../user/entities/user-profile.entity.intf.ts | 17 ++++ .../user-security-profile.entity.intf.ts | 6 ++ .../src/user/entities/user.entity.ts | 89 +++++++++++++++++++ 4 files changed, 117 insertions(+) create mode 100644 packages/stark-core/src/user/entities/index.ts create mode 100644 packages/stark-core/src/user/entities/user-profile.entity.intf.ts create mode 100644 packages/stark-core/src/user/entities/user-security-profile.entity.intf.ts create mode 100644 packages/stark-core/src/user/entities/user.entity.ts diff --git a/packages/stark-core/src/user/entities/index.ts b/packages/stark-core/src/user/entities/index.ts new file mode 100644 index 0000000000..f736638320 --- /dev/null +++ b/packages/stark-core/src/user/entities/index.ts @@ -0,0 +1,5 @@ +"use strict"; + +export * from "./user.entity"; +export * from "./user-profile.entity.intf"; +export * from "./user-security-profile.entity.intf"; diff --git a/packages/stark-core/src/user/entities/user-profile.entity.intf.ts b/packages/stark-core/src/user/entities/user-profile.entity.intf.ts new file mode 100644 index 0000000000..478e302383 --- /dev/null +++ b/packages/stark-core/src/user/entities/user-profile.entity.intf.ts @@ -0,0 +1,17 @@ +"use strict"; + +export interface StarkUserProfile { + username: string; + firstName: string; + lastName: string; + email?: string; + phone?: string; + language: string; + referenceNumber?: string; + isAnonymous?: boolean; + + /** + * This property will contain any additional details for the user profile returned by the backend + */ + custom?: object; +} diff --git a/packages/stark-core/src/user/entities/user-security-profile.entity.intf.ts b/packages/stark-core/src/user/entities/user-security-profile.entity.intf.ts new file mode 100644 index 0000000000..402eb30825 --- /dev/null +++ b/packages/stark-core/src/user/entities/user-security-profile.entity.intf.ts @@ -0,0 +1,6 @@ +"use strict"; + +export interface StarkUserSecurityProfile { + roles: string[]; + workpost?: string; +} diff --git a/packages/stark-core/src/user/entities/user.entity.ts b/packages/stark-core/src/user/entities/user.entity.ts new file mode 100644 index 0000000000..4b32ead843 --- /dev/null +++ b/packages/stark-core/src/user/entities/user.entity.ts @@ -0,0 +1,89 @@ +"use strict"; + +import { autoserialize } from "cerialize"; +import { StarkUserProfile } from "./user-profile.entity.intf"; +import { StarkUserSecurityProfile } from "./user-security-profile.entity.intf"; +import { StarkResource } from "../../http/entities/index"; +import { IsArray, IsBoolean, IsDefined, IsEmail, IsString, ValidateIf } from "class-validator"; + +export class StarkUser implements StarkUserProfile, StarkUserSecurityProfile, StarkResource { + @IsDefined() + @IsString() + @autoserialize + public uuid: string; + + @IsDefined() + @IsString() + @autoserialize + public username: string; + + @IsDefined() + @IsString() + @autoserialize + public firstName: string; + + @IsDefined() + @IsString() + @autoserialize + public lastName: string; + + // @ValidateIf((user: StarkUser) => typeof user.email !== "undefined" && user.email !== null) + @IsEmail() + @autoserialize + public email?: string; + + // @ValidateIf((user: StarkUser) => typeof user.phone !== "undefined" && user.phone !== null) + @IsString() + @autoserialize + public phone?: string; + + // @ValidateIf((user: StarkUser) => typeof user.language !== "undefined" && user.language !== null) + @IsString() + @autoserialize + public language: string; + + // @ValidateIf((user: StarkUser) => typeof user.selectedLanguage !== "undefined" && user.selectedLanguage !== null) + @IsString() + @autoserialize + public selectedLanguage?: string; + + // @ValidateIf((user: StarkUser) => typeof user.referenceNumber !== "undefined" && user.referenceNumber !== null) + @IsString() + @autoserialize + public referenceNumber?: string; + + @IsDefined() + @IsArray() + @autoserialize + public roles: string[] = []; + + @ValidateIf((user: StarkUser) => typeof user.workpost !== "undefined" && user.workpost !== null) + @IsString() + @autoserialize + public workpost?: string; + + @ValidateIf((user: StarkUser) => typeof user.isAnonymous !== "undefined" && user.isAnonymous !== null) + @IsBoolean() + @autoserialize + public isAnonymous?: boolean; + + @autoserialize + public custom?: object; + + /** + * Extract the properties coming in the "details" object. + * This is a callback method provided by cerialize in order to post-process the de-serialized json object. + * @param instance - Instantiated object with its properties already set as defined via the serializer annotations + * @param json - Raw json object retrieved from the http call + * @link https://confluence.prd.nbb/display/jag/REST+-+How-to+configure+the+user+profile+resource + */ + public static OnDeserialized(instance: StarkUser, json: any): void { + if (json.details) { + instance.language = json.details.language; + instance.firstName = json.details.firstName; + instance.lastName = json.details.lastName; + instance.email = json.details.mail; + instance.referenceNumber = json.details.referenceNumber; + } + } +}