Skip to content

Commit

Permalink
feat: guest factory
Browse files Browse the repository at this point in the history
  • Loading branch information
Defferrard authored and spaenleh committed Jul 29, 2024
1 parent 6cd94eb commit cd905fa
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 25 deletions.
12 changes: 1 addition & 11 deletions src/itemLogin/itemLogin.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { DiscriminatedItem } from '@/item/item.js';
import { Member } from '@/member/member.js';
import { UUID } from '@/types.js';

export enum ItemLoginSchemaType {
Expand All @@ -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;
};
}
26 changes: 19 additions & 7 deletions src/member/factory.ts
Original file line number Diff line number Diff line change
@@ -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<T extends MemberType>(baseAccount: Partial<BaseAccount> & {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> = {},
): 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<CompleteGuest> & Pick<CompleteGuest, 'itemLoginSchema'>,
): CompleteGuest => ({
...baseAccountFactory({ type: MemberType.Guest }),
...g,
});
27 changes: 20 additions & 7 deletions src/member/member.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -53,22 +54,34 @@ export const buildMemberExtra = (extra: Partial<MemberExtra>) => ({
...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());

0 comments on commit cd905fa

Please sign in to comment.