Skip to content

Commit

Permalink
feat: guest and account member type
Browse files Browse the repository at this point in the history
  • Loading branch information
Defferrard committed Jul 19, 2024
1 parent 16cdb81 commit 6d21f87
Show file tree
Hide file tree
Showing 13 changed files with 86 additions and 47 deletions.
4 changes: 2 additions & 2 deletions src/action/action.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Context } from '@/enums/context.js';
import { DiscriminatedItem } from '@/item/item.js';
import { ItemMembership } from '@/itemMembership/itemMembership.js';
import { Member } from '@/member/member.js';
import { Account, Member } from '@/member/member.js';

export type Action = {
id: string;
item?: DiscriminatedItem | null;
member?: Member | null;
account?: Account | null;
view: Context | 'Unknown';
type: string;
extra: { [key: string]: unknown };
Expand Down
2 changes: 1 addition & 1 deletion src/action/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { faker } from '@faker-js/faker';
export const ActionFactory = (a: Partial<Action> = {}): Action => ({
id: faker.string.uuid(),
// member and item default to null
member: null,
account: null,
item: null,
view: faker.helpers.arrayElement(Object.values(Context)),
type: faker.lorem.word(),
Expand Down
8 changes: 4 additions & 4 deletions src/app/app.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Publisher } from './publisher.js';
import { DiscriminatedItem } from '@/item/item.js';
import { Member } from '@/member/member.js';
import { Account, Member } from '@/member/member.js';
import { UUID } from '@/types.js';

export type AppExtra = {
Expand All @@ -24,7 +24,7 @@ export interface AppIdentification {
}

export type AuthTokenSubject = {
memberId?: Member['id'];
accountId?: Account['id'];
itemId: DiscriminatedItem['id'];
origin: string;
} & AppIdentification; // from the graasp client/app wrapper // from the app itself
Expand All @@ -34,7 +34,7 @@ type Data = { [key: string]: unknown };
export type AppAction<T extends Data = Data> = {
id: string;
item: DiscriminatedItem;
member: Member;
account: Account;
type: string;
data: T;
createdAt: string;
Expand All @@ -49,7 +49,7 @@ export type AppData<T extends Data = Data> = {
id: string;
item: DiscriminatedItem;
creator: Member | null;
member: Member;
account: Account;
type: string;
visibility: `${AppDataVisibility}` | AppDataVisibility;
data: T;
Expand Down
6 changes: 3 additions & 3 deletions src/chat/chat.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { DiscriminatedItem } from '../item/item.js';
import { Member } from '../member/member.js';
import { Account, Member } from '../member/member.js';
import { UUID } from '../types.js';
import { MentionStatus } from './mentions.js';

export type ChatMessage = {
id: UUID;
item: DiscriminatedItem;
creator: Member | null;
creator: Account | null;
createdAt: string;
updatedAt: string;
body: string;
Expand Down Expand Up @@ -51,7 +51,7 @@ export type DeleteChatMessageParamType = {
export type ChatMention = {
id: UUID;
message: ChatMessage;
member: Member;
account: Account;
createdAt: string;
updatedAt: string;
status: MentionStatus;
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ export * from './itemLike/itemLike.js';
* Item Login
*/
export * from './itemLogin/itemLogin.js';
export * from './itemLogin/factory.js';

/**
* Item Memberships
Expand Down
14 changes: 14 additions & 0 deletions src/itemLogin/factory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { ItemLoginSchema, ItemLoginSchemaType } from './itemLogin.js';
import { faker } from '@faker-js/faker';

export function itemLoginSchemaFactory(
itemLoginSchema: Partial<ItemLoginSchema> & Pick<ItemLoginSchema, 'item'>,
): ItemLoginSchema {
return {
id: faker.string.uuid(),
type: ItemLoginSchemaType.Username,
createdAt: faker.date.anytime().toISOString(),
updatedAt: faker.date.anytime().toISOString(),
...itemLoginSchema,
};
}
10 changes: 0 additions & 10 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 @@ -17,12 +16,3 @@ export interface ItemLoginSchema {
createdAt: string;
updatedAt: string;
}

export type ItemLogin = {
id: UUID;
member: Member;
itemLoginSchema: ItemLoginSchema;
password?: string;
createdAt: string;
updatedAt: string;
};
2 changes: 1 addition & 1 deletion src/itemMembership/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const ItemMembershipFactory = (
id: faker.string.uuid(),
createdAt: faker.date.anytime().toISOString(),
updatedAt: faker.date.anytime().toISOString(),
member: MemberFactory(im.member),
account: MemberFactory(im.account),
permission: faker.helpers.enumValue(PermissionLevel),
...im,
});
4 changes: 2 additions & 2 deletions src/itemMembership/itemMembership.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { PermissionLevel } from '@/enums/permissionLevel/permissionLevel.js';
import { DiscriminatedItem } from '@/item/item.js';
import { Member } from '@/member/member.js';
import { Account, Member } from '@/member/member.js';
import { UUID } from '@/types.js';

export interface ItemMembership {
id: UUID;
member: Member;
account: Account;
item: DiscriminatedItem;
permission: PermissionLevel;
creator?: Member | null;
Expand Down
1 change: 0 additions & 1 deletion src/member/constants.ts

This file was deleted.

33 changes: 26 additions & 7 deletions src/member/factory.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,40 @@
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,
});
10 changes: 4 additions & 6 deletions src/member/member.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { beforeEach, describe, expect, it, vi } from 'vitest';

import { isPseudoMember } from './member.js';
import { MemberType, isPseudoMember } from './member.js';

describe('Member Util Tests', () => {
beforeEach(() => {
Expand All @@ -9,16 +9,14 @@ describe('Member Util Tests', () => {

describe('isPseudoMember', () => {
it('check successfully member is pseudonymized for false values', () => {
const res1 = isPseudoMember({ email: 'mail' });
const res1 = isPseudoMember({ type: MemberType.Individual });
expect(res1).toBeFalsy();
const res2 = isPseudoMember({ email: '[email protected]' });
const res2 = isPseudoMember({ type: MemberType.Group });
expect(res2).toBeFalsy();
const res3 = isPseudoMember({ email: '[email protected]' });
expect(res3).toBeFalsy();
});

it('check successfully member is pseudonymized for true values', () => {
const res3 = isPseudoMember({ email: '[email protected]' });
const res3 = isPseudoMember({ type: MemberType.Guest });
expect(res3).toBeTruthy();
});
});
Expand Down
38 changes: 28 additions & 10 deletions src/member/member.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
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 All @@ -24,6 +24,7 @@ export type PublicProfile = {
export enum MemberType {
Individual = 'individual',
Group = 'group',
Guest = 'guest',
}

type MemberExtra = {
Expand All @@ -40,22 +41,39 @@ 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 Account = {
id: UUID;
name: string;
};

export type Member = Account & {
email: 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());
export function isPseudoMember(member: { type: MemberType }) {
return member.type === MemberType.Guest;
}

0 comments on commit 6d21f87

Please sign in to comment.