Skip to content
This repository has been archived by the owner on Apr 19, 2023. It is now read-only.

Commit

Permalink
✨ Add account deactivate (fixed #1350)
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandChowdhary committed Nov 9, 2020
1 parent dc1a370 commit 48ba176
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/config/configuration.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export interface Configuration {
accessTokenExpiry: string;
passwordPwnedCheck: boolean;
unusedRefreshTokenExpiryDays: number;
inactiveUserDeleteDays: number;
};

email: {
Expand Down
1 change: 1 addition & 0 deletions src/config/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const configuration: Configuration = {
accessTokenExpiry: process.env.ACCESS_TOKEN_EXPIRY ?? '1h',
passwordPwnedCheck: !!process.env.PASSWORD_PWNED_CHECK,
unusedRefreshTokenExpiryDays: int(process.env.DELETE_EXPIRED_SESSIONS, 30),
inactiveUserDeleteDays: int(process.env.INACTIVE_USER_DELETE_DAYS, 30),
},
email: {
name: process.env.EMAIL_NAME ?? 'Staart',
Expand Down
2 changes: 1 addition & 1 deletion src/modules/api-keys/api-keys.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ export class ApiKeysService {
const scopes: Record<string, string> = {};
scopes[`read-info`] = 'Read user details';
scopes[`write-info`] = 'Update user details';
scopes[`delete`] = 'Delete user';
scopes[`deactivate`] = 'Deactivate user';

scopes[`write-membership-*`] = 'Create new groups';
scopes[`read-membership-*`] = 'Read group memberships';
Expand Down
16 changes: 16 additions & 0 deletions src/modules/tasks/tasks.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,20 @@ export class TasksService {
if (deleted.count)
this.logger.debug(`Deleted ${deleted.count} expired sessions`);
}

@Cron(CronExpression.EVERY_DAY_AT_2PM)
async deleteInactiveUsers() {
const now = new Date();
const inactiveUserDeleteDays =
this.configService.get<number>('security.inactiveUserDeleteDays') ?? 30;
now.setDate(now.getDate() - inactiveUserDeleteDays);
const deleted = await this.prisma.users.deleteMany({
where: {
active: false,
sessions: { every: { updatedAt: { lte: now } } },
},
});
if (deleted.count)
this.logger.debug(`Deleted ${deleted.count} inactive users`);
}
}
4 changes: 2 additions & 2 deletions src/modules/users/users.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ export class UserController {
}

@Delete(':id')
@Scopes('user-{id}:delete')
@Scopes('user-{id}:deactivate')
async remove(@Param('id', ParseIntPipe) id: number): Promise<Expose<users>> {
return this.usersService.deleteUser(Number(id));
return this.usersService.deactivateUser(Number(id));
}

@Post(':id/merge-request')
Expand Down
5 changes: 3 additions & 2 deletions src/modules/users/users.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,10 @@ export class UsersService {
return this.prisma.expose<users>(user);
}

async deleteUser(id: number): Promise<Expose<users>> {
const user = await this.prisma.users.delete({
async deactivateUser(id: number): Promise<Expose<users>> {
const user = await this.prisma.users.update({
where: { id },
data: { active: false },
});
return this.prisma.expose<users>(user);
}
Expand Down

0 comments on commit 48ba176

Please sign in to comment.