-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BC-5834-Add-Dashboard-Entities-Deletion-to-the-Main-User-Deletion-Use…
…-Case (#4622) * add method in dashboard repo * add dashboard service * modify service, add some test * add test for dashboardService * Update apps/server/src/modules/learnroom/service/dashboard.service.spec.ts Co-authored-by: Bartosz Nowicki <[email protected]> * Update apps/server/src/modules/deletion/uc/deletion-request.uc.ts Co-authored-by: Sergej Hoffmann <[email protected]> * Update apps/server/src/modules/learnroom/service/dashboard.service.ts Co-authored-by: Sergej Hoffmann <[email protected]> * changes after revieew * change name of variable * add imports * fix imports * add test to DashboardGridElementModelRepo --------- Co-authored-by: Bartosz Nowicki <[email protected]> Co-authored-by: Sergej Hoffmann <[email protected]>
- Loading branch information
1 parent
e72dd50
commit 32720d3
Showing
13 changed files
with
359 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,4 +5,5 @@ export { | |
CourseService, | ||
RoomsService, | ||
CourseGroupService, | ||
DashboardService, | ||
} from './service'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
apps/server/src/modules/learnroom/service/dashboard.service.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
import { createMock, DeepMocked } from '@golevelup/ts-jest'; | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { DashboardElementRepo, IDashboardRepo, UserRepo } from '@shared/repo'; | ||
import { setupEntities, userFactory } from '@shared/testing'; | ||
import { LearnroomMetadata, LearnroomTypes } from '@shared/domain/types'; | ||
import { DashboardEntity, GridElement } from '@shared/domain/entity'; | ||
import { DashboardService } from '.'; | ||
|
||
const learnroomMock = (id: string, name: string) => { | ||
return { | ||
getMetadata(): LearnroomMetadata { | ||
return { | ||
id, | ||
type: LearnroomTypes.Course, | ||
title: name, | ||
shortTitle: name.substr(0, 2), | ||
displayColor: '#ACACAC', | ||
}; | ||
}, | ||
}; | ||
}; | ||
|
||
describe(DashboardService.name, () => { | ||
let module: TestingModule; | ||
let userRepo: DeepMocked<UserRepo>; | ||
let dashboardRepo: IDashboardRepo; | ||
let dashboardElementRepo: DeepMocked<DashboardElementRepo>; | ||
let dashboardService: DeepMocked<DashboardService>; | ||
|
||
beforeAll(async () => { | ||
await setupEntities(); | ||
module = await Test.createTestingModule({ | ||
providers: [ | ||
DashboardService, | ||
{ | ||
provide: UserRepo, | ||
useValue: createMock<UserRepo>(), | ||
}, | ||
{ | ||
provide: 'DASHBOARD_REPO', | ||
useValue: createMock<DashboardService>(), | ||
}, | ||
{ | ||
provide: DashboardElementRepo, | ||
useValue: createMock<DashboardElementRepo>(), | ||
}, | ||
], | ||
}).compile(); | ||
dashboardService = module.get(DashboardService); | ||
userRepo = module.get(UserRepo); | ||
dashboardRepo = module.get('DASHBOARD_REPO'); | ||
dashboardElementRepo = module.get(DashboardElementRepo); | ||
}); | ||
|
||
afterAll(async () => { | ||
await module.close(); | ||
}); | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
describe('when deleting by userId', () => { | ||
const setup = () => { | ||
const user = userFactory.buildWithId(); | ||
userRepo.findById.mockResolvedValue(user); | ||
|
||
return { user }; | ||
}; | ||
|
||
it('should call dashboardRepo.getUsersDashboard', async () => { | ||
const { user } = setup(); | ||
const spy = jest.spyOn(dashboardRepo, 'getUsersDashboard'); | ||
|
||
await dashboardService.deleteDashboardByUserId(user.id); | ||
|
||
expect(spy).toHaveBeenCalledWith(user.id); | ||
}); | ||
|
||
it('should call dashboardElementRepo.deleteByDashboardId', async () => { | ||
const { user } = setup(); | ||
jest.spyOn(dashboardRepo, 'getUsersDashboard').mockResolvedValueOnce( | ||
new DashboardEntity('dashboardId', { | ||
grid: [ | ||
{ | ||
pos: { x: 1, y: 2 }, | ||
gridElement: GridElement.FromPersistedReference('elementId', learnroomMock('referenceId', 'Mathe')), | ||
}, | ||
], | ||
userId: 'userId', | ||
}) | ||
); | ||
const spy = jest.spyOn(dashboardElementRepo, 'deleteByDashboardId'); | ||
|
||
await dashboardService.deleteDashboardByUserId(user.id); | ||
|
||
expect(spy).toHaveBeenCalledWith('dashboardId'); | ||
}); | ||
|
||
it('should call dashboardRepo.deleteDashboardByUserId', async () => { | ||
const { user } = setup(); | ||
const spy = jest.spyOn(dashboardRepo, 'deleteDashboardByUserId'); | ||
|
||
await dashboardService.deleteDashboardByUserId(user.id); | ||
|
||
expect(spy).toHaveBeenCalledWith(user.id); | ||
}); | ||
|
||
it('should delete users dashboard', async () => { | ||
const { user } = setup(); | ||
jest.spyOn(dashboardRepo, 'deleteDashboardByUserId').mockImplementation(() => Promise.resolve(1)); | ||
|
||
const result = await dashboardService.deleteDashboardByUserId(user.id); | ||
|
||
expect(result).toEqual(1); | ||
}); | ||
}); | ||
}); |
19 changes: 19 additions & 0 deletions
19
apps/server/src/modules/learnroom/service/dashboard.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { Inject, Injectable } from '@nestjs/common'; | ||
import { EntityId } from '@shared/domain/types'; | ||
import { IDashboardRepo, DashboardElementRepo } from '@shared/repo'; | ||
|
||
@Injectable() | ||
export class DashboardService { | ||
constructor( | ||
@Inject('DASHBOARD_REPO') private readonly dashboardRepo: IDashboardRepo, | ||
private readonly dashboardElementRepo: DashboardElementRepo | ||
) {} | ||
|
||
async deleteDashboardByUserId(userId: EntityId): Promise<number> { | ||
const usersDashboard = await this.dashboardRepo.getUsersDashboard(userId); | ||
await this.dashboardElementRepo.deleteByDashboardId(usersDashboard.id); | ||
const result = await this.dashboardRepo.deleteDashboardByUserId(userId); | ||
|
||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.