Skip to content

Commit

Permalink
update task ownership at user deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeh committed Jul 15, 2024
1 parent e909ec6 commit f671653
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 5 deletions.
18 changes: 13 additions & 5 deletions api/src/modules/tasks/tasks.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,8 @@ import {
UpdateTaskDto,
} from 'modules/tasks/dto/update-task.dto';
import { GetReportsDto } from 'modules/tasks/dto/get-reports.dto';
import {
ErrorRecord,
TaskReportService,
} from 'modules/tasks/task-report.service';
import { ImportTaskError } from './types/import-task-error.type';
import { TaskReportService } from 'modules/tasks/task-report.service';
import { ImportTaskError } from 'modules/tasks/types/import-task-error.type';

@Injectable()
export class TasksService extends AppBaseService<
Expand Down Expand Up @@ -153,4 +150,15 @@ export class TasksService extends AppBaseService<
errors as ImportTaskError[],
);
}

async updateTaskOwner(oldUserId: string, newUserId: string): Promise<void> {
const tasks: Task[] = await this.taskRepository.find({
where: { userId: oldUserId },
});
if (!tasks.length) return;
await this.taskRepository.update(
{ userId: oldUserId },
{ userId: newUserId },
);
}
}
3 changes: 3 additions & 0 deletions api/src/modules/users/users.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import { Public } from 'decorators/public.decorator';
import { ResetPasswordDto } from 'modules/authentication/dto/reset-password.dto';
import { GetUser } from 'decorators/get-user.decorator';
import { ScenariosService } from 'modules/scenarios/scenarios.service';
import { TasksService } from 'modules/tasks/tasks.service';

@ApiTags(userResource.className)
@Controller(`/api/v1/users`)
Expand All @@ -58,6 +59,7 @@ export class UsersController {
constructor(
public readonly service: UsersService,
private readonly scenarioService: ScenariosService,
private readonly taskService: TasksService,
private readonly accessControl: AccessControl,
) {}

Expand Down Expand Up @@ -251,6 +253,7 @@ export class UsersController {
userIdToDelete,
requestingUserId,
);
await this.taskService.updateTaskOwner(userIdToDelete, requestingUserId);
return this.service.deleteUser(userIdToDelete);
}
}
2 changes: 2 additions & 0 deletions api/src/modules/users/users.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ import { Role } from 'modules/authorization/roles/role.entity';
import { UserRepository } from 'modules/users/user.repository';
import { AuthorizationModule } from 'modules/authorization/authorization.module';
import { ScenariosModule } from 'modules/scenarios/scenarios.module';
import { TasksModule } from 'modules/tasks/tasks.module';

@Module({
imports: [
TypeOrmModule.forFeature([User, Role]),
forwardRef(() => AuthenticationModule),
AuthorizationModule,
ScenariosModule,
TasksModule,
],
providers: [UsersService, UserCommand, UserRepository],
controllers: [UsersController],
Expand Down
24 changes: 24 additions & 0 deletions api/test/e2e/users/users.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
createScenarioIntervention,
createSourcingLocation,
createSourcingRecord,
createTask,
createUser,
} from '../../entity-mocks';
import { Scenario } from '../../../src/modules/scenarios/scenario.entity';
Expand All @@ -28,6 +29,7 @@ import {
import { SourcingRecord } from '../../../src/modules/sourcing-records/sourcing-record.entity';
import { PERMISSIONS } from 'modules/authorization/permissions/permissions.enum';
import { JSONAPIUserData } from '../../../src/modules/users/user.entity';
import { Task } from '../../../src/modules/tasks/task.entity';

/**
* Tests for the UsersModule.
Expand Down Expand Up @@ -467,5 +469,27 @@ describe('UsersModule (e2e)', () => {
expect(scenariosOfNewUser).toHaveLength(2);
expect(scenariosOfDeletedUser).toHaveLength(0);
});
test('A admin user should be able to delete a user, and task should be assigned to the deleting user', async () => {
const userToDelete = await createUser({ email: '[email protected]' });
const newAdminUser = await setupTestUser(testApplication, ROLES.ADMIN, {
email: '[email protected]',
});
for (const _ of [1, 2, 3, 4, 5]) {
await createTask({ userId: userToDelete.id });
}
const response = await request(testApplication.getHttpServer())
.delete(`/api/v1/users/${userToDelete.id}`)
.set('Authorization', `Bearer ${newAdminUser.jwtToken}`);

expect(response.status).toEqual(200);
const tasks = await dataSource.getRepository(Task).find();
const taskOfDeletedUser = await dataSource
.getRepository(Task)
.find({ where: { userId: userToDelete.id } });
expect(taskOfDeletedUser).toHaveLength(0);
tasks.forEach((task: Task) =>
expect(task.userId).toEqual(newAdminUser.user.id),
);
});
});
});

0 comments on commit f671653

Please sign in to comment.