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-4208 - BC-5116 - fix switchSchool by calling teamsRepo directly #4402

Merged
merged 10 commits into from
Sep 13, 2023
3 changes: 3 additions & 0 deletions apps/server/src/apps/server.app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { enableOpenApiDocs } from '@shared/controller/swagger';
import { Mail, MailService } from '@shared/infra/mail';
import { LegacyLogger, Logger } from '@src/core/logger';
import { AccountService } from '@src/modules/account/services/account.service';
import { TeamService } from '@src/modules/teams/service/team.service';
import { AccountValidationService } from '@src/modules/account/services/account.validation.service';
import { AccountUc } from '@src/modules/account/uc/account.uc';
import { CollaborativeStorageUc } from '@src/modules/collaborative-storage/uc/collaborative-storage.uc';
Expand Down Expand Up @@ -76,6 +77,8 @@ async function bootstrap() {
feathersExpress.services['nest-account-uc'] = nestApp.get(AccountUc);
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-unsafe-assignment
feathersExpress.services['nest-collaborative-storage-uc'] = nestApp.get(CollaborativeStorageUc);
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment,@typescript-eslint/no-unsafe-member-access
feathersExpress.services['nest-team-service'] = nestApp.get(TeamService);
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-unsafe-assignment
feathersExpress.services['nest-orm'] = orm;

Expand Down
2 changes: 2 additions & 0 deletions apps/server/src/modules/server/server.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import { VideoConferenceApiModule } from '@src/modules/video-conference/video-co
import connectRedis from 'connect-redis';
import session from 'express-session';
import { RedisClient } from 'redis';
import { TeamsApiModule } from '@src/modules/teams/teams-api.module';
import { ServerController } from './controller/server.controller';
import { serverConfig } from './server.config';

Expand Down Expand Up @@ -72,6 +73,7 @@ const serverModules = [
UserLoginMigrationApiModule,
BoardApiModule,
GroupApiModule,
TeamsApiModule,
];

export const defaultMikroOrmOptions: MikroOrmModuleSyncOptions = {
Expand Down
12 changes: 4 additions & 8 deletions apps/server/src/modules/teams/service/team.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,12 @@ export class TeamService {
public async deleteUserDataFromTeams(userId: EntityId): Promise<number> {
const teams = await this.teamsRepo.findByUserId(userId);

const updatedTeams: TeamEntity[] = teams.map((team: TeamEntity) => {
return {
...team,
userIds: team.userIds.filter((u) => u.userId.id !== userId),
teamUsers: team.userIds.filter((u) => u.userId.id !== userId),
};
teams.forEach((team) => {
team.userIds = team.userIds.filter((u) => u.userId.id !== userId);
});

await this.teamsRepo.save(updatedTeams);
await this.teamsRepo.save(teams);

return updatedTeams.length;
return teams.length;
}
}
10 changes: 10 additions & 0 deletions apps/server/src/modules/teams/teams-api.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Module } from '@nestjs/common';
import { TeamsModule } from '@src/modules/teams/teams.module';

@Module({
imports: [TeamsModule],
providers: [],
controllers: [],
exports: [],
})
export class TeamsApiModule {}
6 changes: 3 additions & 3 deletions src/services/sync/strategies/TSP/SchoolChange.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ const invalidateUser = async (app, user) => {
const deleteUser = (app, user) => {
const userService = app.service('usersModel');
const accountService = app.service('nest-account-service');
const teamsService = app.service('/teams');
const teamService = app.service('nest-team-service');
return Promise.all([
userService.remove({ _id: user._id }),
accountService.deleteByUserId(user._id.toString()),
teamsService.updateMany({ 'userIds.userId': { $in: [user._id] } }, { $pull: { userIds: { userId: user._id } } })
teamService.deleteUserDataFromTeams(user._id.toString()),
]);
};

Expand Down Expand Up @@ -84,7 +84,7 @@ const switchSchool = async (app, currentUser, createUserMethod) => {
await deleteUser(app, currentUser);
return newUser;
} catch (err) {
logError(`Something went wrong during switching school for user: ${err}`);
logError(`Something went wrong during switching school for user (${currentUser.sourceOptions.tspUid})`, err);
return null;
}
};
Expand Down
58 changes: 58 additions & 0 deletions test/services/sync/strategies/TSP/SchoolChange.integration.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
const { expect } = require('chai');

const appPromise = require('../../../../../src/app');
const { setupNestServices, closeNestServices } = require('../../../../utils/setup.nest.services');

const testObjects = require('../../../helpers/testObjects')(appPromise());

const { deleteUser } = require('../../../../../src/services/sync/strategies/TSP/SchoolChange');

const userRepo = require('../../../../../src/components/user/repo/user.repo');
const { NotFound } = require('../../../../../src/errors');

describe('SchooolChange API integration tests', () => {
let app;
let server;
let nestServices;

let nestAccountService;
let teamService;

before(async () => {
app = await appPromise();
server = app.listen(0);
nestServices = await setupNestServices(app);
nestAccountService = app.service('nest-account-service');
teamService = app.service('teams');
});

after(async () => {
await server.close();
await closeNestServices(nestServices);
});

afterEach(async () => {
await testObjects.cleanup();
});

describe('deleteUser', () => {
it('should delete user, users account and delete user from teams', async () => {
const team = await testObjects.createTestTeamWithOwner({ roles: 'teacher' });
const credentials = { username: team.user.email, password: `${Date.now()}` };
await testObjects.createTestAccount(credentials, 'local', team.user);

await deleteUser(app, team.user);

await expect(userRepo.getUser(team.user._id)).to.be.rejectedWith(NotFound);
const deletedAccount = await nestAccountService.findByUserId(team.user._id);
expect(deletedAccount).to.be.equal(null);
const deletedUserTeams = await teamService.find({
query: {
$limit: false,
userIds: { $elemMatch: { userId: team.user._id } },
},
});
expect(deletedUserTeams.total).to.be.equal(0);
});
});
});
5 changes: 5 additions & 0 deletions test/utils/setup.nest.services.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ const {
} = require('../../dist/apps/server/modules/account/services/account.validation.service');
const { DB_PASSWORD, DB_URL, DB_USERNAME } = require('../../dist/apps/server/config/database.config');
const { ALL_ENTITIES } = require('../../dist/apps/server/shared/domain/entity/all-entities');
const { TeamService } = require('../../dist/apps/server/modules/teams/service/team.service');
const { TeamsApiModule } = require('../../dist/apps/server/modules/teams/teams-api.module');

const setupNestServices = async (app) => {
const module = await Test.createTestingModule({
Expand All @@ -28,17 +30,20 @@ const setupNestServices = async (app) => {
}),
ConfigModule.forRoot({ ignoreEnvFile: true, ignoreEnvVars: true, isGlobal: true }),
AccountApiModule,
TeamsApiModule,
],
}).compile();
const nestApp = await module.createNestApplication().init();
const orm = nestApp.get(MikroORM);
const accountUc = nestApp.get(AccountUc);
const accountService = nestApp.get(AccountService);
const accountValidationService = nestApp.get(AccountValidationService);
const teamService = nestApp.get(TeamService);

app.services['nest-account-uc'] = accountUc;
app.services['nest-account-service'] = accountService;
app.services['nest-account-validation-service'] = accountValidationService;
app.services['nest-team-service'] = teamService;
app.services['nest-orm'] = orm;

return { nestApp, orm, accountUc, accountService };
Expand Down