Skip to content

Commit

Permalink
Merge pull request #113 from amosproj/91-backend-alert-deactivation
Browse files Browse the repository at this point in the history
91 backend alert deactivation
  • Loading branch information
heskil authored Dec 2, 2024
2 parents 9e13836 + 7d49c64 commit 4bf4552
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 132 deletions.
82 changes: 32 additions & 50 deletions apps/backend/src/app/alerting/alerting.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,57 +132,7 @@ describe('AlertingController (e2e)', () => {
expect.objectContaining(createAlertTypeDto)
);
});
it('PATCH /alerting/type/:id/activate/user - should activate alert type by user', async () => {
const alertTypeId = 'not-active-id';

await request(app.getHttpServer())
.patch(`/alerting/type/${alertTypeId}/activate/user`)
.expect(200);

expect(mockAlertTypeRepository.save).toHaveBeenCalledWith({
...mockedAlertTypeEntity,
user_active: true,
master_active: false,
});
});
it('PATCH /alerting/type/:id/deactivate/user - should deactivate alert type by user', async () => {
const alertTypeId = 'active-id';

await request(app.getHttpServer())
.patch(`/alerting/type/${alertTypeId}/deactivate/user`)
.expect(200);

expect(mockAlertTypeRepository.save).toHaveBeenCalledWith({
...mockedAlertTypeEntity,
user_active: false,
master_active: true,
});
});
it('PATCH /alerting/type/:id/activate/admin - should activate alert type by admin', async () => {
const alertTypeId = 'not-active-id';

await request(app.getHttpServer())
.patch(`/alerting/type/${alertTypeId}/activate/admin`)
.expect(200);

expect(mockAlertTypeRepository.save).toHaveBeenCalledWith({
...mockedAlertTypeEntity,
user_active: false,
master_active: true,
});
});
it('PATCH /alerting/type/:id/deactivate/admin - should deactivate alert type by admin', async () => {
const alertTypeId = 'active-id';

await request(app.getHttpServer())
.patch(`/alerting/type/${alertTypeId}/deactivate/admin`)
.expect(200);

expect(mockAlertTypeRepository.save).toHaveBeenCalledWith({
...mockedAlertTypeEntity,
master_active: false,
});
});
it('GET /alerting - should filter alerts by backupId', async () => {
const response = await request(app.getHttpServer())
.get('/alerting')
Expand Down Expand Up @@ -256,4 +206,36 @@ describe('AlertingController (e2e)', () => {
})
);
});

it('PATCH /alerting/type/:alertTypeId/admin - should activate alert type by admin', async () => {
const alertTypeId = 'not-active-id';
const alertStatusDto = { status: true };

await request(app.getHttpServer())
.patch(`/alerting/type/${alertTypeId}/admin`)
.send(alertStatusDto)
.expect(200);

expect(mockAlertTypeRepository.save).toHaveBeenCalledWith({
...mockedAlertTypeEntity,
master_active: true,
user_active: false,
});
});

it('PATCH /alerting/type/:alertTypeId/user - should activate alert type by user', async () => {
const alertTypeId = 'not-active-id';
const alertStatusDto = { status: true };

await request(app.getHttpServer())
.patch(`/alerting/type/${alertTypeId}/user`)
.send(alertStatusDto)
.expect(200);

expect(mockAlertTypeRepository.save).toHaveBeenCalledWith({
...mockedAlertTypeEntity,
master_active: false,
user_active: true,
});
});
});
41 changes: 21 additions & 20 deletions apps/backend/src/app/alerting/alerting.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { CreateAlertTypeDto } from './dto/createAlertType.dto';
import { AlertTypeEntity } from './entity/alertType.entity';
import { CreateSizeAlertDto } from './dto/alerts/createSizeAlert.dto';
import { Alert } from './entity/alerts/alert';
import { AlertStatusDto } from './dto/alertStatus.dto';

@Controller('alerting')
export class AlertingController {
Expand All @@ -35,32 +36,32 @@ export class AlertingController {
await this.alertingService.createAlertType(createAlertTypeDto);
}

@Patch('type/:alertTypeId/activate/user')
@Patch('type/:alertTypeId/user')
@ApiOperation({ summary: 'Activate Alert Type by user.' })
@ApiNotFoundResponse({ description: 'Alert type not found' })
async userActivateAlertType(@Param('alertTypeId') alertTypeId: string) {
await this.alertingService.userActivateAlertType(alertTypeId);
@ApiBody({ type: AlertStatusDto })
async userChangeActiveStatusAlertType(
@Param('alertTypeId') alertTypeId: string,
@Body() alertStatusDto: AlertStatusDto
) {
await this.alertingService.userChangeActiveStatusAlertType(
alertTypeId,
alertStatusDto.status
);
}

@Patch('type/:alertTypeId/deactivate/user')
@ApiOperation({ summary: 'Deactivate Alert Type by user' })
@ApiNotFoundResponse({ description: 'Alert type not found' })
async userDeactivateAlertType(@Param('alertTypeId') alertTypeId: string) {
await this.alertingService.userDeactivateAlertType(alertTypeId);
}

@Patch('type/:alertTypeId/activate/admin')
@Patch('type/:alertTypeId/admin')
@ApiOperation({ summary: 'Activate Alert Type by admin.' })
@ApiNotFoundResponse({ description: 'Alert type not found' })
async adminActivateAlertType(@Param('alertTypeId') alertTypeId: string) {
await this.alertingService.adminActivateAlertType(alertTypeId);
}

@Patch('type/:alertTypeId/deactivate/admin')
@ApiOperation({ summary: 'Deactivate Alert Type by admin' })
@ApiNotFoundResponse({ description: 'Alert type not found' })
async adminDeactivateAlertType(@Param('alertTypeId') alertTypeId: string) {
await this.alertingService.adminDeactivateAlertType(alertTypeId);
@ApiBody({ type: AlertStatusDto })
async adminChangeActiveStatusAlertType(
@Param('alertTypeId') alertTypeId: string,
@Body() alertStatusDto: AlertStatusDto
) {
await this.alertingService.adminChangeActiveStatusAlertType(
alertTypeId,
alertStatusDto.status
);
}

@Get('type')
Expand Down
97 changes: 52 additions & 45 deletions apps/backend/src/app/alerting/alerting.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,51 +189,6 @@ describe('AlertingService', () => {
});
});
});
describe('activate/deactivate', () => {
it('should activate alert type by user', async () => {
const alertTypeId = 'not-active-id';

await service.userActivateAlertType(alertTypeId);

expect(alertTypeRepository.save).toHaveBeenCalledWith({
...mockedAlertTypeEntity,
user_active: true,
master_active: false,
});
});
it('should deactivate alert type by user', async () => {
const alertTypeId = 'active-id';

await service.userDeactivateAlertType(alertTypeId);

expect(alertTypeRepository.save).toHaveBeenCalledWith({
...mockedAlertTypeEntity,
user_active: false,
master_active: true,
});
});
it('should activate alert type by admin', async () => {
const alertTypeId = 'not-active-id';

await service.adminActivateAlertType(alertTypeId);

expect(alertTypeRepository.save).toHaveBeenCalledWith({
...mockedAlertTypeEntity,
user_active: false,
master_active: true,
});
});
it('should deactivate alert type by admin', async () => {
const alertTypeId = 'active-id';

await service.adminDeactivateAlertType(alertTypeId);

expect(alertTypeRepository.save).toHaveBeenCalledWith({
...mockedAlertTypeEntity,
master_active: false,
});
});
});

describe('findAllAlerts', () => {
it('should return all alerts', async () => {
Expand Down Expand Up @@ -281,4 +236,56 @@ describe('AlertingService', () => {
expect(mailService.sendAlertMail).toHaveBeenCalledWith(alert);
});
});

describe('adminChangeActiveStatusAlertType', () => {
it('should activate alert type by admin', async () => {
const alertTypeId = 'not-active-id';
const alertStatusDto = { status: true };

await service.adminChangeActiveStatusAlertType(alertTypeId, true);

expect(alertTypeRepository.save).toHaveBeenCalledWith({
...mockedAlertTypeEntity,
master_active: true,
user_active: false,
});
});

it('should deactivate alert type by admin', async () => {
const alertTypeId = 'active-id';

await service.adminChangeActiveStatusAlertType(alertTypeId, false);

expect(alertTypeRepository.save).toHaveBeenCalledWith({
...mockedAlertTypeEntity,
master_active: false,
});
});
});

describe('userChangeActiveStatusAlertType', () => {
it('should activate alert type by admin', async () => {
const alertTypeId = 'not-active-id';
const alertStatusDto = { status: true };

await service.userChangeActiveStatusAlertType(alertTypeId, true);

expect(alertTypeRepository.save).toHaveBeenCalledWith({
...mockedAlertTypeEntity,
master_active: false,
user_active: true,
});
});

it('should deactivate alert type by admin', async () => {
const alertTypeId = 'active-id';

await service.userChangeActiveStatusAlertType(alertTypeId, false);

expect(alertTypeRepository.save).toHaveBeenCalledWith({
...mockedAlertTypeEntity,
user_active: false,
});
});
});
});
24 changes: 7 additions & 17 deletions apps/backend/src/app/alerting/alerting.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,27 +41,15 @@ export class AlertingService {
return await this.alertTypeRepository.save(createAlertTypeDto);
}

async userActivateAlertType(alertTypeId: string) {
async userChangeActiveStatusAlertType(alertTypeId: string, status: boolean) {
const alertType = await this.findAlertTypeByIdOrThrow(alertTypeId);
alertType.user_active = true;
alertType.user_active = status;
return await this.alertTypeRepository.save(alertType);
}

async userDeactivateAlertType(alertTypeId: string) {
async adminChangeActiveStatusAlertType(alertTypeId: string, status: boolean) {
const alertType = await this.findAlertTypeByIdOrThrow(alertTypeId);
alertType.user_active = false;
return await this.alertTypeRepository.save(alertType);
}

async adminActivateAlertType(alertTypeId: string) {
const alertType = await this.findAlertTypeByIdOrThrow(alertTypeId);
alertType.master_active = true;
return await this.alertTypeRepository.save(alertType);
}

async adminDeactivateAlertType(alertTypeId: string) {
const alertType = await this.findAlertTypeByIdOrThrow(alertTypeId);
alertType.master_active = false;
alertType.master_active = status;
return await this.alertTypeRepository.save(alertType);
}

Expand All @@ -84,7 +72,9 @@ export class AlertingService {
}

async getAllAlerts(backupId?: string, days?: number): Promise<Alert[]> {
const where: FindOptionsWhere<Alert> = { alertType: { user_active: true, master_active: true } };
const where: FindOptionsWhere<Alert> = {
alertType: { user_active: true, master_active: true },
};
if (backupId) {
where.backup = { id: backupId };
}
Expand Down
10 changes: 10 additions & 0 deletions apps/backend/src/app/alerting/dto/alertStatus.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsBoolean } from 'class-validator';

export class AlertStatusDto {
@ApiProperty({
description: 'Status of the alert, if it is active or not',
example: true,
})
status!: boolean;
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export class CreateSizeAlertDto {
description: 'Size of the Backup, which is the reason for the alert',
})
size!: number;

@ApiProperty({
description:
'Reference size to the value of the Backup, in which comparison the alert was triggered',
Expand Down

0 comments on commit 4bf4552

Please sign in to comment.