From cd905fa7dd89cf493051860f463500caf9156247 Mon Sep 17 00:00:00 2001 From: Jeremy Defferrard Date: Mon, 1 Jul 2024 16:05:34 +0200 Subject: [PATCH] feat: guest factory --- src/itemLogin/itemLogin.ts | 12 +----------- src/member/factory.ts | 26 +++++++++++++++++++------- src/member/member.ts | 27 ++++++++++++++++++++------- 3 files changed, 40 insertions(+), 25 deletions(-) diff --git a/src/itemLogin/itemLogin.ts b/src/itemLogin/itemLogin.ts index 5af6b8b5..9763b4b5 100644 --- a/src/itemLogin/itemLogin.ts +++ b/src/itemLogin/itemLogin.ts @@ -1,5 +1,4 @@ import { DiscriminatedItem } from '@/item/item.js'; -import { Member } from '@/member/member.js'; import { UUID } from '@/types.js'; export enum ItemLoginSchemaType { @@ -16,13 +15,4 @@ export interface ItemLoginSchema { type: `${ItemLoginSchemaType}` | ItemLoginSchemaType; createdAt: string; updatedAt: string; -} - -export type ItemLogin = { - id: UUID; - member: Member; - itemLoginSchema: ItemLoginSchema; - password?: string; - createdAt: string; - updatedAt: string; -}; +} \ No newline at end of file diff --git a/src/member/factory.ts b/src/member/factory.ts index 06e47a29..80c55f2a 100644 --- a/src/member/factory.ts +++ b/src/member/factory.ts @@ -1,21 +1,33 @@ -import { CompleteMember, MemberType } from './member.js'; +import { BaseAccount, CompleteGuest, CompleteMember, MemberType } from './member.js'; import { faker } from '@faker-js/faker'; +function baseAccountFactory(baseAccount: Partial & {type: T}): BaseAccount & {type: T} { + return { + id: faker.string.uuid(), + name: faker.person.fullName(), + createdAt: faker.date.anytime().toISOString(), + updatedAt: faker.date.anytime().toISOString(), + ...baseAccount, + }; + } + export const MemberFactory = ( m: Partial = {}, ): CompleteMember => ({ - id: faker.string.uuid(), - name: faker.person.fullName(), email: faker.internet.email(), - createdAt: faker.date.anytime().toISOString(), - updatedAt: faker.date.anytime().toISOString(), extra: faker.helpers.arrayElement([ { lang: faker.helpers.arrayElement(['en', 'fr', 'de']) }, {}, ]), - // todo: handle other member type when useful in backend - type: MemberType.Individual, enableSaveActions: m.enableSaveActions ?? true, isValidated: m.isValidated ?? true, + ...baseAccountFactory({ type: MemberType.Individual }), ...m, }); + +export const GuestFactory = ( + g: Partial & Pick, +): CompleteGuest => ({ + ...baseAccountFactory({ type: MemberType.Guest }), + ...g, +}); diff --git a/src/member/member.ts b/src/member/member.ts index b3b7bc1c..72433319 100644 --- a/src/member/member.ts +++ b/src/member/member.ts @@ -1,5 +1,6 @@ import { PSEUDO_USER_MAIL_REGEX } from './constants.js'; import { EmailFrequency } from '@/chat/mentions.js'; +import { ItemLoginSchema } from '@/itemLogin/itemLogin.js'; import { UUID } from '@/types.js'; export type Password = string; @@ -53,22 +54,34 @@ export const buildMemberExtra = (extra: Partial) => ({ ...extra, }); -export type Member = { +export type BaseAccount = { id: UUID; name: string; - email: string; + type: `${MemberType}` | MemberType; + createdAt: string; + updatedAt: string; + lastAuthenticatedAt?: string; }; -export type CompleteMember = Member & { - type: `${MemberType}` | MemberType; +export type CompleteAccount = CompleteMember | CompleteGuest; + +export type Member = { + id: UUID; + name: string; +}; +export type CompleteMember = BaseAccount & { + type: MemberType.Individual; + email: string; extra: MemberExtra; enableSaveActions: boolean; userAgreementsDate?: string; - createdAt: string; - updatedAt: string; - lastAuthenticatedAt?: string; + isValidated: boolean; }; +export type CompleteGuest = BaseAccount & { + type: MemberType.Guest; + itemLoginSchema: ItemLoginSchema; +}; export const isPseudoMember = (member: { email: string }) => PSEUDO_USER_MAIL_REGEX.test(member.email.toLowerCase());