From da20649f5e99954db7216ba295d46a5f1b14ab30 Mon Sep 17 00:00:00 2001 From: Andres Pinto Date: Fri, 15 Mar 2024 10:55:12 -0400 Subject: [PATCH 1/3] feat: kickoff middleware workspaces --- .../domains/workspace-team-user.domain.ts | 48 ++++++++++++++++ .../domains/workspace-team.domain.ts | 5 ++ .../domains/workspace-user.domain.ts | 57 +++++++++++++++++++ .../workspaces/domains/workspaces.domain.ts | 5 ++ .../models/workspace-team-users.model.ts | 2 +- .../workspaces/models/workspace.model.ts | 4 ++ .../repositories/team.repository.ts | 28 +++++++++ .../repositories/workspaces.repository.ts | 39 ++++++++++++- src/modules/workspaces/workspaces.module.ts | 2 + 9 files changed, 186 insertions(+), 4 deletions(-) create mode 100644 src/modules/workspaces/domains/workspace-team-user.domain.ts create mode 100644 src/modules/workspaces/domains/workspace-user.domain.ts diff --git a/src/modules/workspaces/domains/workspace-team-user.domain.ts b/src/modules/workspaces/domains/workspace-team-user.domain.ts new file mode 100644 index 000000000..2a77c693b --- /dev/null +++ b/src/modules/workspaces/domains/workspace-team-user.domain.ts @@ -0,0 +1,48 @@ +import { User } from '../../user/user.domain'; +import { WorkspaceTeam } from './workspace-team.domain'; +import { WorkspaceTeamUserAttributes } from '../attributes/workspace-team-users.attributes'; + +export class WorkspaceTeamUser implements WorkspaceTeamUserAttributes { + id: string; + teamId: string; + memberId: string; + team?: WorkspaceTeam; + createdAt: Date; + updatedAt: Date; + + constructor({ + id, + teamId, + memberId, + team, + createdAt, + updatedAt, + }: WorkspaceTeamUserAttributes & { team?: WorkspaceTeam }) { + this.id = id; + this.teamId = teamId; + this.memberId = memberId; + this.team = team; + this.createdAt = createdAt; + this.updatedAt = updatedAt; + } + + static build( + workspaceTeamUser: WorkspaceTeamUserAttributes & { + team?: WorkspaceTeam; + member?: User; + }, + ): WorkspaceTeamUser { + return new WorkspaceTeamUser(workspaceTeamUser); + } + + toJSON() { + return { + id: this.id, + teamId: this.teamId, + memberId: this.memberId, + team: this.team ? this.team.toJSON() : undefined, + createdAt: this.createdAt, + updatedAt: this.updatedAt, + }; + } +} diff --git a/src/modules/workspaces/domains/workspace-team.domain.ts b/src/modules/workspaces/domains/workspace-team.domain.ts index ff90620b4..7b53cea62 100644 --- a/src/modules/workspaces/domains/workspace-team.domain.ts +++ b/src/modules/workspaces/domains/workspace-team.domain.ts @@ -1,3 +1,4 @@ +import { User } from '../../user/user.domain'; import { WorkspaceTeamAttributes } from '../attributes/workspace-team.attributes'; export class WorkspaceTeam implements WorkspaceTeamAttributes { @@ -24,6 +25,10 @@ export class WorkspaceTeam implements WorkspaceTeamAttributes { this.updatedAt = updatedAt; } + isUserManager(userUuid: User['uuid']) { + return userUuid === this.managerId; + } + static build(teamAttributes: WorkspaceTeamAttributes): WorkspaceTeam { return new WorkspaceTeam(teamAttributes); } diff --git a/src/modules/workspaces/domains/workspace-user.domain.ts b/src/modules/workspaces/domains/workspace-user.domain.ts new file mode 100644 index 000000000..d36d4c102 --- /dev/null +++ b/src/modules/workspaces/domains/workspace-user.domain.ts @@ -0,0 +1,57 @@ +import { WorkspaceUserAttributes } from '../attributes/workspace-users.attributes'; + +export class WorkspaceUser implements WorkspaceUserAttributes { + id: string; + memberId: string; + key: string; + workspaceId: string; + spaceLimit: bigint; + driveUsage: bigint; + backupsUsage: bigint; + deactivated: boolean; + createdAt: Date; + updatedAt: Date; + + constructor({ + id, + memberId, + key, + workspaceId, + spaceLimit, + driveUsage, + backupsUsage, + deactivated, + createdAt, + updatedAt, + }: WorkspaceUserAttributes) { + this.id = id; + this.memberId = memberId; + this.key = key; + this.workspaceId = workspaceId; + this.spaceLimit = spaceLimit; + this.driveUsage = driveUsage; + this.backupsUsage = backupsUsage; + this.deactivated = deactivated; + this.createdAt = createdAt; + this.updatedAt = updatedAt; + } + + static build(workspaceUser: WorkspaceUserAttributes): WorkspaceUser { + return new WorkspaceUser(workspaceUser); + } + + toJSON() { + return { + id: this.id, + memberId: this.memberId, + key: this.key, + workspaceId: this.workspaceId, + spaceLimit: this.spaceLimit.toString(), + driveUsage: this.driveUsage.toString(), + backupsUsage: this.backupsUsage.toString(), + deactivated: this.deactivated, + createdAt: this.createdAt, + updatedAt: this.updatedAt, + }; + } +} diff --git a/src/modules/workspaces/domains/workspaces.domain.ts b/src/modules/workspaces/domains/workspaces.domain.ts index 869c45585..82c4d3131 100644 --- a/src/modules/workspaces/domains/workspaces.domain.ts +++ b/src/modules/workspaces/domains/workspaces.domain.ts @@ -1,3 +1,4 @@ +import { UserAttributes } from '../../user/user.attributes'; import { WorkspaceAttributes } from '../attributes/workspace.attributes'; export class Workspace implements WorkspaceAttributes { @@ -40,6 +41,10 @@ export class Workspace implements WorkspaceAttributes { return new Workspace(user); } + isUserOwner(userUuid: UserAttributes['uuid']) { + return userUuid === this.ownerId; + } + toJSON() { return { id: this.id, diff --git a/src/modules/workspaces/models/workspace-team-users.model.ts b/src/modules/workspaces/models/workspace-team-users.model.ts index 768f462ba..ea3ca5002 100644 --- a/src/modules/workspaces/models/workspace-team-users.model.ts +++ b/src/modules/workspaces/models/workspace-team-users.model.ts @@ -14,7 +14,7 @@ import { WorkspaceTeamUserAttributes } from '../attributes/workspace-team-users. @Table({ underscored: true, timestamps: true, - tableName: 'teams_users', + tableName: 'workspace_teams_users', }) export class WorkspaceTeamUserModel extends Model diff --git a/src/modules/workspaces/models/workspace.model.ts b/src/modules/workspaces/models/workspace.model.ts index fae2e96fc..6bb342ddb 100644 --- a/src/modules/workspaces/models/workspace.model.ts +++ b/src/modules/workspaces/models/workspace.model.ts @@ -6,6 +6,7 @@ import { PrimaryKey, ForeignKey, BelongsTo, + HasMany, } from 'sequelize-typescript'; import { UserModel } from '../../user/user.model'; import { WorkspaceUserModel } from './workspace-users.model'; @@ -60,6 +61,9 @@ export class WorkspaceModel extends Model implements WorkspaceAttributes { @Column(DataType.UUID) workspaceUserId: string; + @HasMany(() => WorkspaceUserModel) + workspaceUsers: WorkspaceUserModel[]; + @Column createdAt: Date; diff --git a/src/modules/workspaces/repositories/team.repository.ts b/src/modules/workspaces/repositories/team.repository.ts index f585067fb..e8615f088 100644 --- a/src/modules/workspaces/repositories/team.repository.ts +++ b/src/modules/workspaces/repositories/team.repository.ts @@ -8,6 +8,8 @@ import { WorkspaceTeamModel } from '../models/workspace-team.model'; import { WorkspaceTeamUserModel } from '../models/workspace-team-users.model'; import { WorkspaceTeam } from '../domains/workspace-team.domain'; import { WorkspaceTeamAttributes } from '../attributes/workspace-team.attributes'; +import { UserAttributes } from '../../user/user.attributes'; +import { WorkspaceTeamUser } from '../domains/workspace-team-user.domain'; @Injectable() export class SequelizeWorkspaceTeamRepository { @@ -44,6 +46,26 @@ export class SequelizeWorkspaceTeamRepository { return result.map((teamUser) => User.build({ ...teamUser.member })); } + async getTeamUserAndTeamByTeamId( + userUuid: UserAttributes['uuid'], + teamId: WorkspaceTeamAttributes['id'], + ) { + const team = await this.teamModel.findOne({ + where: { id: teamId }, + include: { + required: false, + model: WorkspaceTeamUserModel, + where: { memberId: userUuid }, + }, + }); + return { + team: team ? this.toDomain(team) : null, + teamUser: team.teamUsers[0] + ? this.teamUserToDomain(team.teamUsers[0]) + : null, + }; + } + async getTeamById( teamId: WorkspaceTeamAttributes['id'], ): Promise { @@ -85,4 +107,10 @@ export class SequelizeWorkspaceTeamRepository { ...model.toJSON(), }); } + + teamUserToDomain(model: WorkspaceTeamUserModel): WorkspaceTeamUser { + return WorkspaceTeamUser.build({ + ...model.toJSON(), + }); + } } diff --git a/src/modules/workspaces/repositories/workspaces.repository.ts b/src/modules/workspaces/repositories/workspaces.repository.ts index a177438cf..07e5e34a6 100644 --- a/src/modules/workspaces/repositories/workspaces.repository.ts +++ b/src/modules/workspaces/repositories/workspaces.repository.ts @@ -4,9 +4,11 @@ import { FindOrCreateOptions, Transaction } from 'sequelize/types'; import { WorkspaceAttributes } from '../attributes/workspace.attributes'; import { Workspace } from '../domains/workspaces.domain'; import { WorkspaceModel } from '../models/workspace.model'; +import { WorkspaceUserModel } from '../models/workspace-users.model'; +import { WorkspaceUser } from '../domains/workspace-user.domain'; export interface WorkspaceRepository { - findById(id: number): Promise; + findById(id: WorkspaceAttributes['id']): Promise; findByOwner(ownerId: Workspace['ownerId']): Promise; createTransaction(): Promise; findOrCreate(opts: FindOrCreateOptions): Promise<[Workspace | null, boolean]>; @@ -35,8 +37,10 @@ export class SequelizeWorkspaceRepository implements WorkspaceRepository { constructor( @InjectModel(WorkspaceModel) private modelWorkspace: typeof WorkspaceModel, + @InjectModel(WorkspaceUserModel) + private modelWorkspaceUser: typeof WorkspaceUserModel, ) {} - async findById(id: number): Promise { + async findById(id: WorkspaceAttributes['id']): Promise { const workspace = await this.modelWorkspace.findByPk(id); return workspace ? this.toDomain(workspace) : null; } @@ -75,6 +79,29 @@ export class SequelizeWorkspaceRepository implements WorkspaceRepository { return workspaces.map((workspace) => this.toDomain(workspace)); } + async findWorkspaceAndUser( + userUuid: string, + workspaceId: string, + ): Promise<{ workspace: Workspace; workspaceUser: WorkspaceUser } | null> { + const workspace = await this.modelWorkspace.findOne({ + where: { id: workspaceId }, + include: { + required: false, + model: WorkspaceUserModel, + where: { + memberId: userUuid, + }, + }, + }); + + return { + workspace: workspace ? this.toDomain(workspace) : null, + workspaceUser: workspace?.workspaceUsers + ? this.workspaceUserToDomain(workspace.workspaceUsers[0]) + : null, + }; + } + async findAllByWithPagination( where: any, limit = 20, @@ -110,8 +137,14 @@ export class SequelizeWorkspaceRepository implements WorkspaceRepository { }); } + workspaceUserToDomain(model: WorkspaceUserModel): WorkspaceUser { + return WorkspaceUser.build({ + ...model?.toJSON(), + }); + } + toModel(domain: Workspace): Partial { - return domain.toJSON(); + return domain?.toJSON(); } } export { WorkspaceModel }; diff --git a/src/modules/workspaces/workspaces.module.ts b/src/modules/workspaces/workspaces.module.ts index 81840128f..0c4de72ab 100644 --- a/src/modules/workspaces/workspaces.module.ts +++ b/src/modules/workspaces/workspaces.module.ts @@ -11,6 +11,7 @@ import { SequelizeWorkspaceTeamRepository } from './repositories/team.repository import { BridgeModule } from '../../externals/bridge/bridge.module'; import { WorkspaceTeamModel } from './models/workspace-team.model'; import { WorkspaceTeamUserModel } from './models/workspace-team-users.model'; +import { WorkspaceGuard } from './guards/workspaces.guard'; @Module({ imports: [ @@ -30,6 +31,7 @@ import { WorkspaceTeamUserModel } from './models/workspace-team-users.model'; WorkspacesUsecases, SequelizeWorkspaceTeamRepository, SequelizeWorkspaceRepository, + WorkspaceGuard, ], exports: [WorkspacesUsecases, SequelizeModule], }) From 60f15309c8d564042ff182047b0f532081b83217 Mon Sep 17 00:00:00 2001 From: Andres Pinto Date: Mon, 18 Mar 2024 21:54:35 -0400 Subject: [PATCH 2/3] feat: add tests for middleware --- .../domains/workspace-team.domain.ts | 4 +- .../workspaces/domains/workspaces.domain.ts | 6 +- .../repositories/team.repository.ts | 8 +- .../repositories/workspaces.repository.ts | 13 +-- test/fixtures.spec.ts | 79 +++++++++++++++++++ test/fixtures.ts | 62 +++++++++++++++ 6 files changed, 159 insertions(+), 13 deletions(-) diff --git a/src/modules/workspaces/domains/workspace-team.domain.ts b/src/modules/workspaces/domains/workspace-team.domain.ts index 7b53cea62..89f11498c 100644 --- a/src/modules/workspaces/domains/workspace-team.domain.ts +++ b/src/modules/workspaces/domains/workspace-team.domain.ts @@ -25,8 +25,8 @@ export class WorkspaceTeam implements WorkspaceTeamAttributes { this.updatedAt = updatedAt; } - isUserManager(userUuid: User['uuid']) { - return userUuid === this.managerId; + isUserManager(user: User) { + return user.uuid === this.managerId; } static build(teamAttributes: WorkspaceTeamAttributes): WorkspaceTeam { diff --git a/src/modules/workspaces/domains/workspaces.domain.ts b/src/modules/workspaces/domains/workspaces.domain.ts index 82c4d3131..0dae51992 100644 --- a/src/modules/workspaces/domains/workspaces.domain.ts +++ b/src/modules/workspaces/domains/workspaces.domain.ts @@ -1,4 +1,4 @@ -import { UserAttributes } from '../../user/user.attributes'; +import { User } from '../../user/user.domain'; import { WorkspaceAttributes } from '../attributes/workspace.attributes'; export class Workspace implements WorkspaceAttributes { @@ -41,8 +41,8 @@ export class Workspace implements WorkspaceAttributes { return new Workspace(user); } - isUserOwner(userUuid: UserAttributes['uuid']) { - return userUuid === this.ownerId; + isUserOwner(user: User) { + return user.uuid === this.ownerId; } toJSON() { diff --git a/src/modules/workspaces/repositories/team.repository.ts b/src/modules/workspaces/repositories/team.repository.ts index e8615f088..747fd77ad 100644 --- a/src/modules/workspaces/repositories/team.repository.ts +++ b/src/modules/workspaces/repositories/team.repository.ts @@ -49,7 +49,10 @@ export class SequelizeWorkspaceTeamRepository { async getTeamUserAndTeamByTeamId( userUuid: UserAttributes['uuid'], teamId: WorkspaceTeamAttributes['id'], - ) { + ): Promise<{ + team: WorkspaceTeam | null; + teamUser: WorkspaceTeamUser | null; + }> { const team = await this.teamModel.findOne({ where: { id: teamId }, include: { @@ -58,9 +61,10 @@ export class SequelizeWorkspaceTeamRepository { where: { memberId: userUuid }, }, }); + return { team: team ? this.toDomain(team) : null, - teamUser: team.teamUsers[0] + teamUser: team?.teamUsers?.[0] ? this.teamUserToDomain(team.teamUsers[0]) : null, }; diff --git a/src/modules/workspaces/repositories/workspaces.repository.ts b/src/modules/workspaces/repositories/workspaces.repository.ts index 07e5e34a6..5c4def2e5 100644 --- a/src/modules/workspaces/repositories/workspaces.repository.ts +++ b/src/modules/workspaces/repositories/workspaces.repository.ts @@ -82,21 +82,22 @@ export class SequelizeWorkspaceRepository implements WorkspaceRepository { async findWorkspaceAndUser( userUuid: string, workspaceId: string, - ): Promise<{ workspace: Workspace; workspaceUser: WorkspaceUser } | null> { + ): Promise<{ + workspace: Workspace | null; + workspaceUser: WorkspaceUser | null; + }> { const workspace = await this.modelWorkspace.findOne({ where: { id: workspaceId }, include: { - required: false, model: WorkspaceUserModel, - where: { - memberId: userUuid, - }, + where: { memberId: userUuid }, + required: false, }, }); return { workspace: workspace ? this.toDomain(workspace) : null, - workspaceUser: workspace?.workspaceUsers + workspaceUser: workspace?.workspaceUsers?.[0] ? this.workspaceUserToDomain(workspace.workspaceUsers[0]) : null, }; diff --git a/test/fixtures.spec.ts b/test/fixtures.spec.ts index 7468946ba..06620663a 100644 --- a/test/fixtures.spec.ts +++ b/test/fixtures.spec.ts @@ -267,4 +267,83 @@ describe('Testing fixtures tests', () => { expect(limit.value).toEqual('0'); }); }); + + describe("Workspace's fixture", () => { + it('When it generates a workspace, then the identifier should be random', () => { + const workspace = fixtures.newWorkspace(); + const otherWorkspace = fixtures.newWorkspace(); + + expect(workspace.id).toBeTruthy(); + expect(workspace.id).not.toBe(otherWorkspace.id); + }); + + it('When it generates a workspace, then the ownerId should be random', () => { + const workspace = fixtures.newWorkspace(); + const otherWorkspace = fixtures.newWorkspace(); + + expect(workspace.ownerId).toBeTruthy(); + expect(workspace.ownerId).not.toBe(otherWorkspace.ownerId); + }); + + it('When it generates a workspace with an owner, then the ownerId should match the owner', () => { + const owner = fixtures.newUser(); + const workspace = fixtures.newWorkspace({ owner }); + + expect(workspace.ownerId).toBe(owner.uuid); + }); + + it('When it generates a workspace, then the createdAt should be equal or less than updatedAt', () => { + const workspace = fixtures.newWorkspace(); + + expect(workspace.createdAt.getTime()).toBeLessThanOrEqual( + workspace.updatedAt.getTime(), + ); + }); + + it('When it generates a workspace, then the setupCompleted should be a boolean value', () => { + const workspace = fixtures.newWorkspace(); + + expect(typeof workspace.setupCompleted).toBe('boolean'); + }); + }); + + describe("WorkspaceTeam's fixture", () => { + it('When it generates a workspace team, then the identifier should be random', () => { + const team = fixtures.newWorkspaceTeam(); + const otherTeam = fixtures.newWorkspaceTeam(); + + expect(team.id).toBeTruthy(); + expect(team.id).not.toBe(otherTeam.id); + }); + + it('When it generates a workspace team, then the workspaceId should be random', () => { + const team = fixtures.newWorkspaceTeam(); + const otherTeam = fixtures.newWorkspaceTeam(); + + expect(team.workspaceId).toBeTruthy(); + expect(team.workspaceId).not.toBe(otherTeam.workspaceId); + }); + + it('When it generates a workspace team with a manager, then the managerId should match the manager', () => { + const manager = fixtures.newUser(); + const team = fixtures.newWorkspaceTeam({ manager }); + + expect(team.managerId).toBe(manager.uuid); + }); + + it('When it generates a workspace team, then the createdAt should be equal or less than updatedAt', () => { + const team = fixtures.newWorkspaceTeam(); + + expect(team.createdAt.getTime()).toBeLessThanOrEqual( + team.updatedAt.getTime(), + ); + }); + + it('When it generates a workspace team, then the name should be populated', () => { + const team = fixtures.newWorkspaceTeam(); + + expect(team.name).toBeTruthy(); + expect(typeof team.name).toBe('string'); + }); + }); }); diff --git a/test/fixtures.ts b/test/fixtures.ts index 3046e7db7..c677856f8 100644 --- a/test/fixtures.ts +++ b/test/fixtures.ts @@ -16,6 +16,8 @@ import { LimitTypes, } from '../src/modules/feature-limit/limits.enum'; import { Limit } from '../src/modules/feature-limit/limit.domain'; +import { Workspace } from '../src/modules/workspaces/domains/workspaces.domain'; +import { WorkspaceTeam } from '../src/modules/workspaces/domains/workspace-team.domain'; export const constants = { BUCKET_ID_LENGTH: 24, @@ -245,3 +247,63 @@ export const newFeatureLimit = (bindTo?: { label: bindTo?.label ?? ('' as LimitLabels), }); }; + +export const newWorkspace = (params?: { + attributes?: Partial; + owner?: User; +}): Workspace => { + const randomCreatedAt = randomDataGenerator.date(); + + const workspace = Workspace.build({ + id: v4(), + ownerId: params?.owner?.uuid || v4(), + address: randomDataGenerator.address(), + name: randomDataGenerator.company(), + description: randomDataGenerator.sentence(), + defaultTeamId: v4(), + workspaceUserId: v4(), + setupCompleted: randomDataGenerator.bool(), + createdAt: randomCreatedAt, + updatedAt: new Date( + randomDataGenerator.date({ + min: randomCreatedAt, + }), + ), + }); + + params?.attributes && + Object.keys(params.attributes).forEach((key) => { + workspace[key] = params.attributes[key]; + }); + + return workspace; +}; + +export const newWorkspaceTeam = (params?: { + attributes?: Partial; + workspaceId?: string; + manager?: User; +}): WorkspaceTeam => { + const randomCreatedAt = randomDataGenerator.date(); + const manager = params?.manager || newUser(); + + const team = WorkspaceTeam.build({ + id: v4(), + workspaceId: params?.workspaceId || v4(), + managerId: manager.uuid, + name: randomDataGenerator.word(), + createdAt: randomCreatedAt, + updatedAt: new Date( + randomDataGenerator.date({ + min: randomCreatedAt, + }), + ), + }); + + params?.attributes && + Object.keys(params.attributes).forEach((key) => { + team[key] = params.attributes[key]; + }); + + return team; +}; From d89fa7ae856e0d5b111162972900203d347994b8 Mon Sep 17 00:00:00 2001 From: Andres Pinto Date: Tue, 19 Mar 2024 17:49:45 -0400 Subject: [PATCH 3/3] chore: added fixture for workspaceUser --- test/fixtures.spec.ts | 43 +++++++++++++++++++++++++++++++++++++++++++ test/fixtures.ts | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) diff --git a/test/fixtures.spec.ts b/test/fixtures.spec.ts index 06620663a..49a203ea7 100644 --- a/test/fixtures.spec.ts +++ b/test/fixtures.spec.ts @@ -346,4 +346,47 @@ describe('Testing fixtures tests', () => { expect(typeof team.name).toBe('string'); }); }); + + describe("WorkspaceUser's fixture", () => { + it('When it generates a workspace user, then the identifier should be random', () => { + const user = fixtures.newWorkspaceUser(); + const otherUser = fixtures.newWorkspaceUser(); + expect(user.id).toBeTruthy(); + expect(user.id).not.toBe(otherUser.id); + }); + + it('When it generates a workspace user, then the workspaceId should be random', () => { + const user = fixtures.newWorkspaceUser(); + const otherUser = fixtures.newWorkspaceUser(); + expect(user.workspaceId).toBeTruthy(); + expect(user.workspaceId).not.toBe(otherUser.workspaceId); + }); + + it('When it generates a workspace user with a specified memberId, then the memberId should match', () => { + const memberId = 'anyId'; + const user = fixtures.newWorkspaceUser({ memberId }); + expect(user.memberId).toBe(memberId); + }); + + it('When it generates a workspace user, then driveUsage and backupsUsage should not exceed spaceLimit', () => { + const user = fixtures.newWorkspaceUser(); + expect(Number(user.driveUsage)).toBeLessThanOrEqual( + BigInt(user.spaceLimit), + ); + expect(BigInt(user.backupsUsage)).toBeLessThanOrEqual( + BigInt(user.spaceLimit), + ); + }); + + it('When it generates a workspace user with custom attributes, then those attributes are set correctly', () => { + const customAttributes = { + deactivated: true, + spaceLimit: BigInt(500), + }; + const user = fixtures.newWorkspaceUser({ attributes: customAttributes }); + + expect(user.deactivated).toBe(customAttributes.deactivated); + expect(user.spaceLimit).toBe(customAttributes.spaceLimit); + }); + }); }); diff --git a/test/fixtures.ts b/test/fixtures.ts index c677856f8..af33d106c 100644 --- a/test/fixtures.ts +++ b/test/fixtures.ts @@ -18,6 +18,7 @@ import { import { Limit } from '../src/modules/feature-limit/limit.domain'; import { Workspace } from '../src/modules/workspaces/domains/workspaces.domain'; import { WorkspaceTeam } from '../src/modules/workspaces/domains/workspace-team.domain'; +import { WorkspaceUser } from '../src/modules/workspaces/domains/workspace-user.domain'; export const constants = { BUCKET_ID_LENGTH: 24, @@ -307,3 +308,36 @@ export const newWorkspaceTeam = (params?: { return team; }; + +export const newWorkspaceUser = (params?: { + workspaceId?: string; + memberId?: string; + attributes?: Partial; +}): WorkspaceUser => { + const randomCreatedAt = randomDataGenerator.date(); + const spaceLimit = randomDataGenerator.natural({ min: 1, max: 1073741824 }); + + const workspaceUser = WorkspaceUser.build({ + id: v4(), + memberId: params?.memberId || v4(), + key: randomDataGenerator.string({ length: 32 }), + workspaceId: params?.workspaceId || v4(), + spaceLimit: BigInt(spaceLimit), + driveUsage: BigInt( + randomDataGenerator.natural({ min: 1, max: spaceLimit }), + ), + backupsUsage: BigInt( + randomDataGenerator.natural({ min: 1, max: spaceLimit }), + ), + deactivated: randomDataGenerator.bool(), + createdAt: randomCreatedAt, + updatedAt: new Date(randomDataGenerator.date({ min: randomCreatedAt })), + }); + + params?.attributes && + Object.keys(params.attributes).forEach((key) => { + workspaceUser[key] = params.attributes[key]; + }); + + return workspaceUser; +};