Skip to content

Commit

Permalink
feat(user): add entities for user module
Browse files Browse the repository at this point in the history
  • Loading branch information
tenretC committed Apr 11, 2018
1 parent 5dac35d commit 38c48fe
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 0 deletions.
5 changes: 5 additions & 0 deletions packages/stark-core/src/user/entities/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"use strict";

export * from "./user.entity";
export * from "./user-profile.entity.intf";
export * from "./user-security-profile.entity.intf";
17 changes: 17 additions & 0 deletions packages/stark-core/src/user/entities/user-profile.entity.intf.ts
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"use strict";

export interface StarkUserSecurityProfile {
roles: string[];
workpost?: string;
}
89 changes: 89 additions & 0 deletions packages/stark-core/src/user/entities/user.entity.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}
}

0 comments on commit 38c48fe

Please sign in to comment.