Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BC-5836 - Add news to KNLDeletion Module #4654

Merged
merged 62 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from 40 commits
Commits
Show all changes
62 commits
Select commit Hold shift + click to select a range
c78fe0c
add finBycreatorId in repo
WojciechGrancow Dec 19, 2023
64830b3
add logger to registrationPin deleteService
WojciechGrancow Dec 29, 2023
a5bae20
add logger to dashboardModule deleteService
WojciechGrancow Dec 29, 2023
8a9247a
add logger to classModule deleteService
WojciechGrancow Dec 29, 2023
b8df859
add logger to courseGroupService for delete method
WojciechGrancow Dec 29, 2023
72627f8
add logger to courseService for delete method
WojciechGrancow Dec 29, 2023
59740db
add logger to FilesModule for removePermision and markFordeletion
WojciechGrancow Dec 29, 2023
e596193
add logger to LessonModule for removeUserData from lessons
WojciechGrancow Dec 29, 2023
1d888f9
add logger to pseudonymService and add logger import in course test
WojciechGrancow Jan 2, 2024
45f886b
add logger to teamService in team module
WojciechGrancow Jan 2, 2024
37e31c7
add logger to userService for deleteUsermethod in user module
WojciechGrancow Jan 2, 2024
6f4f906
add logger to RocketChatUser and to RocketChat Services for delete me…
WojciechGrancow Jan 2, 2024
ca57167
Update apps/server/src/modules/learnroom/service/course.service.spec.ts
WojciechGrancow Jan 4, 2024
9b74b29
add additional metadata to logs during deletion user data for services
WojciechGrancow Jan 4, 2024
1dbfedc
fix test
WojciechGrancow Jan 4, 2024
289175d
Merge branch 'main' into BC-6082-add-logger-to-KNL-module
WojciechGrancow Jan 4, 2024
dce0c7b
some fixes
WojciechGrancow Jan 8, 2024
ba666be
Merge branch 'main' into BC-6082-add-logger-to-KNL-module
WojciechGrancow Jan 8, 2024
93393b9
fix after review
WojciechGrancow Jan 10, 2024
5caf15d
Merge branch 'main' into BC-6082-add-logger-to-KNL-module
WojciechGrancow Jan 10, 2024
4c44540
Merge branch 'main' into BC-6082-add-logger-to-KNL-module
WojciechGrancow Jan 10, 2024
391161e
change logger in teams module
WojciechGrancow Jan 10, 2024
735e876
Merge branch 'main' into BC-5836-Add-News-to-KNLDeletion
sszafGCA Jan 13, 2024
f456dfd
Merge branch 'main' into BC-6082-add-logger-to-KNL-module
WojciechGrancow Jan 14, 2024
21c0639
Merge branch 'main' into BC-6082-add-logger-to-KNL-module
sszafGCA Jan 14, 2024
078a868
Merge remote-tracking branch 'origin/BC-6082-add-logger-to-KNL-module…
sszafGCA Jan 14, 2024
f6e43b8
add import in pseudonym module
WojciechGrancow Jan 14, 2024
8a24e22
Merge branch 'BC-6082-add-logger-to-KNL-module' of https://github.com…
WojciechGrancow Jan 14, 2024
785a35b
Merge remote-tracking branch 'origin/BC-6082-add-logger-to-KNL-module…
sszafGCA Jan 14, 2024
6c1076d
impl
sszafGCA Jan 14, 2024
4a37243
Merge branch 'main' into BC-5836-Add-News-to-KNLDeletion
sszafGCA Jan 14, 2024
a588105
fix lint
sszafGCA Jan 14, 2024
d0eeba3
fix lint #2
sszafGCA Jan 14, 2024
868e880
add module
sszafGCA Jan 14, 2024
1cf70ba
fix tests
sszafGCA Jan 14, 2024
dd6d271
test fix #2
sszafGCA Jan 14, 2024
8bd5843
cover all lines
sszafGCA Jan 14, 2024
6a9ca23
Merge branch 'main' into BC-5836-Add-News-to-KNLDeletion
sszafGCA Jan 15, 2024
dd2e239
Merge branch 'main' into BC-5836-Add-News-to-KNLDeletion
sszafGCA Jan 15, 2024
1e5f865
Merge branch 'main' into BC-5836-Add-News-to-KNLDeletion
sszafGCA Jan 17, 2024
3f09791
Merge branch 'main' into BC-5836-Add-News-to-KNLDeletion
sszafGCA Jan 26, 2024
2b01182
remove updater reference impl
sszafGCA Jan 26, 2024
efa4f3c
fix tests
sszafGCA Jan 26, 2024
03d0794
fix test coverage
sszafGCA Jan 26, 2024
0cc7c8e
fix test coverage
sszafGCA Jan 26, 2024
7569de0
Merge branch 'main' into BC-5836-Add-News-to-KNLDeletion
sszafGCA Jan 26, 2024
43f381a
Merge branch 'main' into BC-5836-Add-News-to-KNLDeletion
sszafGCA Jan 26, 2024
7c76735
fix PR
sszafGCA Jan 28, 2024
db94392
Merge branch 'main' into BC-5836-Add-News-to-KNLDeletion
sszafGCA Jan 28, 2024
a368162
Merge branch 'main' into BC-5836-Add-News-to-KNLDeletion
sszafGCA Jan 29, 2024
3920579
Merge branch 'main' into BC-5836-Add-News-to-KNLDeletion
sszafGCA Jan 29, 2024
30ca040
Merge branch 'main' into BC-5836-Add-News-to-KNLDeletion
WojciechGrancow Jan 30, 2024
dad0f56
Merge branch 'BC-5836-Add-News-to-KNLDeletion' of https://github.com/…
WojciechGrancow Jan 30, 2024
fc58e3f
Merge branch 'main' into BC-5836-Add-News-to-KNLDeletion
WojciechGrancow Jan 30, 2024
f6470d7
add new logging funct. to news module
WojciechGrancow Jan 30, 2024
e490546
Update apps/server/src/shared/repo/news/news.repo.integration.spec.ts
WojciechGrancow Jan 31, 2024
5fecb04
some fixes after review
WojciechGrancow Jan 31, 2024
147fec6
Merge branch 'BC-5836-Add-News-to-KNLDeletion' into BC-5836-and-BC-64…
bn-pass Feb 2, 2024
4438900
Merge branch 'main' into BC-5836-Add-News-to-KNLDeletion
WojciechGrancow Feb 6, 2024
cbdc6fc
change delete user method
WojciechGrancow Feb 6, 2024
70d5b43
Revert "change delete user method"
WojciechGrancow Feb 6, 2024
a71b89c
Merge branch 'BC-5836-and-BC-6424-news-testing' into BC-5836-Add-News…
bn-pass Feb 6, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions apps/server/src/modules/deletion/deletion-api.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { FilesStorageClientModule } from '@modules/files-storage-client';
import { DeletionRequestsController } from './controller/deletion-requests.controller';
import { DeletionExecutionsController } from './controller/deletion-executions.controller';
import { DeletionRequestUc } from './uc';
import { NewsModule } from '../news';

@Module({
imports: [
Expand All @@ -37,6 +38,7 @@ import { DeletionRequestUc } from './uc';
RegistrationPinModule,
FilesStorageClientModule,
TaskModule,
NewsModule,
RocketChatModule.forRoot({
uri: Configuration.get('ROCKET_CHAT_URI') as string,
adminId: Configuration.get('ROCKET_CHAT_ADMIN_ID') as string,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { FilesStorageClientAdapterService } from '@src/modules/files-storage-cli
import { DomainModel } from '@shared/domain/types';
import { TaskService } from '@modules/task';
import { DomainOperationBuilder } from '@shared/domain/builder';
import { NewsUc } from '@src/modules/news/uc';
import { DeletionStatusModel } from '../domain/types';
import { DeletionLogService } from '../services/deletion-log.service';
import { DeletionRequestService } from '../services';
Expand Down Expand Up @@ -124,6 +125,7 @@ describe(DeletionRequestUc.name, () => {
provide: TaskService,
useValue: createMock<TaskService>(),
},
{ provide: NewsUc, useValue: createMock<NewsUc>() },
],
}).compile();

Expand Down Expand Up @@ -472,7 +474,7 @@ describe(DeletionRequestUc.name, () => {

await uc.executeDeletionRequests();

expect(deletionLogService.createDeletionLog).toHaveBeenCalledTimes(13);
expect(deletionLogService.createDeletionLog).toHaveBeenCalledTimes(14);
});
});

Expand Down
17 changes: 16 additions & 1 deletion apps/server/src/modules/deletion/uc/deletion-request.uc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { FilesStorageClientAdapterService } from '@modules/files-storage-client'
import { TaskService } from '@modules/task';
import { DomainOperation } from '@shared/domain/interface';
import { DomainOperationBuilder } from '@shared/domain/builder/domain-operation.builder';
import { NewsUc } from '@src/modules/news/uc';
import { DeletionRequestLogResponseBuilder, DeletionTargetRefBuilder } from '../builder';
import { DeletionRequestBodyProps, DeletionRequestLogResponse, DeletionRequestResponse } from '../controller/dto';
import { DeletionRequest, DeletionLog } from '../domain';
Expand All @@ -42,7 +43,8 @@ export class DeletionRequestUc {
private readonly registrationPinService: RegistrationPinService,
private readonly filesStorageClientAdapterService: FilesStorageClientAdapterService,
private readonly dashboardService: DashboardService,
private readonly taskService: TaskService
private readonly taskService: TaskService,
private readonly newsUc: NewsUc
sszafGCA marked this conversation as resolved.
Show resolved Hide resolved
) {
this.logger.setContext(DeletionRequestUc.name);
}
Expand Down Expand Up @@ -113,6 +115,7 @@ export class DeletionRequestUc {
this.removeUserRegistrationPin(deletionRequest),
this.removeUsersDashboard(deletionRequest),
this.removeUserFromTasks(deletionRequest),
this.removeUsersDataFromNews(deletionRequest),
]);
await this.deletionRequestService.markDeletionRequestAsExecuted(deletionRequest.id);
} catch (error) {
Expand Down Expand Up @@ -294,4 +297,16 @@ export class DeletionRequestUc {
tasksDeleted.deletedCount
);
}

private async removeUsersDataFromNews(deletionRequest: DeletionRequest) {
const newsesModifiedByRemoveCreator = await this.newsUc.deleteCreatorReference(deletionRequest.targetRefId);

await this.logDeletion(
deletionRequest,
DomainModel.NEWS,
DeletionOperationModel.UPDATE,
newsesModifiedByRemoveCreator,
0
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export class NewsResponse {
@ApiProperty({
description: 'Reference to the User that created the News entity',
})
creator: UserInfoResponse;
creator: UserInfoResponse | undefined;

@ApiPropertyOptional({
description: 'Reference to the User that updated the News entity',
Expand Down
13 changes: 11 additions & 2 deletions apps/server/src/modules/news/mapper/news.mapper.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { News } from '@shared/domain/entity';
import { CreateNews, INewsScope, IUpdateNews, NewsTargetModel } from '@shared/domain/types';
import { LogMessageData } from '@src/core/logger';
import { CreateNewsParams, FilterNewsParams, NewsResponse, UpdateNewsParams } from '../controller/dto';
import {
CreateNewsParams,
FilterNewsParams,
NewsResponse,
UpdateNewsParams,
UserInfoResponse,
} from '../controller/dto';
import { SchoolInfoMapper } from './school-info.mapper';
import { TargetInfoMapper } from './target-info.mapper';
import { UserInfoMapper } from './user-info.mapper';
Expand All @@ -10,7 +16,10 @@ export class NewsMapper {
static mapToResponse(news: News): NewsResponse {
const target = TargetInfoMapper.mapToResponse(news.target);
const school = SchoolInfoMapper.mapToResponse(news.school);
const creator = UserInfoMapper.mapToResponse(news.creator);
let creator: UserInfoResponse | undefined;
if (news.creator !== undefined) {
creator = UserInfoMapper.mapToResponse(news.creator);
}
sszafGCA marked this conversation as resolved.
Show resolved Hide resolved

const dto = new NewsResponse({
id: news.id,
Expand Down
56 changes: 56 additions & 0 deletions apps/server/src/modules/news/uc/news.uc.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { Permission } from '@shared/domain/interface';
import { CreateNews, NewsTargetModel } from '@shared/domain/types';
import { NewsRepo } from '@shared/repo';
import { Logger } from '@src/core/logger';
import { FederalStateEntity, Role, SchoolEntity, TeamNews, User } from '@shared/domain/entity';
import { Collection } from '@mikro-orm/core';
import { NewsUc } from './news.uc';

describe('NewsUc', () => {
Expand Down Expand Up @@ -40,6 +42,40 @@ describe('NewsUc', () => {
id: courseTargetId,
},
};
const federalState = new FederalStateEntity({
name: 'string',
abbreviation: 'string',
logoUrl: 'string',
createdAt: new Date(),
updatedAt: new Date(),
});

const school = new SchoolEntity({
_id: schoolId,

name: 'string',
federalState,
});
const creator: User = {
_id: new ObjectId(userId),
id: userId,
email: 'string',
firstName: 'string',
lastName: 'string',
roles: new Collection<Role>(User),
school,
createdAt: new Date(),
updatedAt: new Date(),
resolvePermissions: () => ['a'],
};
const exampleNews: TeamNews = new TeamNews({
title: 'string',
content: 'string',
displayAt,
creator,
school: schoolId,
target: teamTargetId,
});
sszafGCA marked this conversation as resolved.
Show resolved Hide resolved
const pagination = {};

const targets = [
Expand Down Expand Up @@ -75,6 +111,12 @@ describe('NewsUc', () => {
}
throw new NotFoundException();
},
findByCreatorId(creatorId) {
if (creatorId === creator.id) {
return [[exampleNews], 1];
}
return [[undefined], 0];
},
delete() {},
},
},
Expand Down Expand Up @@ -301,4 +343,18 @@ describe('NewsUc', () => {
await expect(service.delete(newsId, anotherUser)).rejects.toThrow(UnauthorizedException);
});
});

describe('deleteCreatorReference', () => {
it('should successfully delete creator reference from news', async () => {
const result = await service.deleteCreatorReference(creator.id);
expect(exampleNews.creator).toBeUndefined();
expect(result).toBe(1);
});

it('should return 0 if news not found', async () => {
const anotherUser = new ObjectId().toHexString();
const result = await service.deleteCreatorReference(anotherUser);
expect(result).toBe(0);
});
});
});
48 changes: 47 additions & 1 deletion apps/server/src/modules/news/uc/news.uc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,20 @@ import { FeathersAuthorizationService } from '@modules/authorization';
import { Injectable } from '@nestjs/common';
import { News } from '@shared/domain/entity';
import { IFindOptions, Permission, SortOrder } from '@shared/domain/interface';
import { Counted, CreateNews, EntityId, INewsScope, IUpdateNews, NewsTargetModel } from '@shared/domain/types';
import {
Counted,
CreateNews,
DomainModel,
EntityId,
INewsScope,
IUpdateNews,
NewsTargetModel,
StatusModel,
} from '@shared/domain/types';
import { NewsRepo, NewsTargetFilter } from '@shared/repo';
import { CrudOperation } from '@shared/types';
import { Logger } from '@src/core/logger';
import { DataDeletionDomainOperationLoggable } from '@shared/common/loggable';
import { NewsCrudOperationLoggable } from '../loggable/news-crud-operation.loggable';

type NewsPermission = Permission.NEWS_VIEW | Permission.NEWS_EDIT;
Expand Down Expand Up @@ -147,6 +157,42 @@ export class NewsUc {
return id;
}

public async deleteCreatorReference(creatorId: EntityId): Promise<number> {
this.logger.info(
new DataDeletionDomainOperationLoggable(
'Deleting user data from News',
DomainModel.NEWS,
creatorId,
StatusModel.PENDING
)
);

const news = await this.newsRepo.findByCreatorId(creatorId);

const newsCount = news[1];
if (newsCount === 0) {
return 0;
}

news[0].forEach((newsEntity) => {
newsEntity.removeCreatorReference(creatorId);
});

await this.newsRepo.save(news[0]);

this.logger.info(
new DataDeletionDomainOperationLoggable(
'Successfully removed user data from News',
DomainModel.NEWS,
creatorId,
StatusModel.FINISHED,
newsCount,
0
)
);
return newsCount;
}

private async getPermittedTargets(userId: EntityId, scope: INewsScope | undefined, permissions: NewsPermission[]) {
let targets: NewsTargetFilter[];

Expand Down
31 changes: 31 additions & 0 deletions apps/server/src/shared/domain/entity/news.entity.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { setupEntities, teamNewsFactory, userFactory } from '@shared/testing';
import { News } from './news.entity';

describe(News.name, () => {
beforeAll(async () => {
await setupEntities();
});

describe('removeCreatorReference', () => {
describe('when called on a news that contains some creator with given refId', () => {
const setup = () => {
const creator = userFactory.buildWithId();
const news = teamNewsFactory.build({
creator,
});

const expectedNews = news;
expectedNews.creator = undefined;

return { creator, news, expectedNews };
};
it('should properly remove this creator reference', () => {
const { creator, news, expectedNews } = setup();

news.removeCreatorReference(creator.id);

expect(news).toEqual(expectedNews);
});
});
});
});
12 changes: 9 additions & 3 deletions apps/server/src/shared/domain/entity/news.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export interface NewsProperties {
content: string;
displayAt: Date;
school: EntityId | SchoolEntity;
creator: EntityId | User;
creator?: EntityId | User;
target: EntityId | NewsTarget;

externalId?: string;
Expand Down Expand Up @@ -61,14 +61,20 @@ export abstract class News extends BaseEntityWithTimestamps {
@ManyToOne(() => SchoolEntity, { fieldName: 'schoolId' })
school!: SchoolEntity;

@ManyToOne('User', { fieldName: 'creatorId' })
creator!: User;
@ManyToOne('User', { fieldName: 'creatorId', nullable: true })
creator?: User | undefined;

@ManyToOne('User', { fieldName: 'updaterId', nullable: true })
updater?: User;

permissions: string[] = [];

public removeCreatorReference(creatorId: EntityId): void {
WojciechGrancow marked this conversation as resolved.
Show resolved Hide resolved
if (creatorId === this.creator?.id) {
this.creator = undefined;
}
}

constructor(props: NewsProperties) {
super();
this.title = props.title;
Expand Down
1 change: 1 addition & 0 deletions apps/server/src/shared/domain/types/domain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ export const enum DomainModel {
TASK = 'task',
TEAMS = 'teams',
USER = 'user',
NEWS = 'news',
}
Loading
Loading