Skip to content

Commit

Permalink
Add api update monitor
Browse files Browse the repository at this point in the history
  • Loading branch information
thongnguyen5 authored and thongnguyendev committed Apr 8, 2024
1 parent 7794e9d commit 6a542e1
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 5 deletions.
61 changes: 61 additions & 0 deletions app/apps/onebox/src/modules/monitor/dto/monitor.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,3 +228,64 @@ export class DeleteMonitorResponseDto {
@ApiResponseProperty()
success: boolean;
}

export class UpdateMonitorDto {
monitorId: string;

@ApiProperty({
example: {
native: true,
internal: true,
erc721: true,
erc20: true,
specific: false,
cryptos: [],
},
})
@ValidateNested()
condition: MonitorConditionDto;

@ApiProperty({
example: {
method: MonitorNotificationMethod.Webhook,
url: 'https://example.com',
authorization: 'Basic dGVzdHVzZXJuYW1lOnRlc3RwYXNzd29yZA==',
secretToken: '',
},
})
@ValidateNested()
@Type(() => NotificationDto, {
keepDiscriminatorProperty: true,
discriminator: {
property: 'method',
subTypes: [
{
name: MonitorNotificationMethod.Webhook,
value: WebhookNotificationDto,
},
{
name: MonitorNotificationMethod.SMS,
value: SMSNotificationDto,
},
{
name: MonitorNotificationMethod.Email,
value: EmailNotificationDto,
},
],
},
})
notification:
| NotificationDto
| WebhookNotificationDto
| SMSNotificationDto
| EmailNotificationDto;

@ApiProperty({ example: 'all' })
type: MonitoringType;

@ApiProperty()
note: string;

@ApiProperty()
tags: string[];
}
28 changes: 24 additions & 4 deletions app/apps/onebox/src/modules/monitor/monitor.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
Get,
Param,
Post,
Put,
Query,
Req,
UseGuards,
Expand All @@ -16,6 +17,7 @@ import {
ApiOperation,
ApiTags,
} from '@nestjs/swagger';
import { Builder } from 'builder-pattern';
import { Request } from 'express';
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
import { User } from '../users/schemas/user.schema';
Expand All @@ -24,6 +26,7 @@ import {
DeleteMonitorDto,
DeleteMonitorResponseDto,
MonitorResponseDto,
UpdateMonitorDto,
} from './dto/monitor.dto';
import { MonitorService } from './monitor.service';

Expand Down Expand Up @@ -71,12 +74,29 @@ export class MonitorController {
@ApiOperation({ summary: 'Delete monitor' })
@ApiBearerAuth('JWT')
@UseGuards(JwtAuthGuard)
@Delete('')
@ApiCreatedResponse({ type: MonitorResponseDto })
@Delete('/:id')
@ApiCreatedResponse({ type: DeleteMonitorResponseDto })
async deleteMonitor(
@Req() req: Request,
@Body() body: DeleteMonitorDto,
@Param('id') monitorId: string,
): Promise<DeleteMonitorResponseDto> {
return await this.monitorService.deleteMonitor(req.user as User, body);
return await this.monitorService.deleteMonitor(
req.user as User,
Builder<DeleteMonitorDto>().monitorId(monitorId).build(),
);
}

@ApiOperation({ summary: 'Update monitor' })
@ApiBearerAuth('JWT')
@UseGuards(JwtAuthGuard)
@Put('/:id')
@ApiOkResponse({ type: MonitorResponseDto })
async updateMonitor(
@Req() req: Request,
@Param('id') monitorId: string,
@Body() body: UpdateMonitorDto,
): Promise<MonitorResponseDto> {
body.monitorId = monitorId;
return await this.monitorService.updateMonitor(req.user as User, body);
}
}
56 changes: 55 additions & 1 deletion app/apps/onebox/src/modules/monitor/monitor.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
DeleteMonitorDto,
DeleteMonitorResponseDto,
MonitorResponseDto,
UpdateMonitorDto,
} from './dto/monitor.dto';

@Injectable()
Expand Down Expand Up @@ -98,7 +99,7 @@ export class MonitorService {
monitor.projectId,
);
if (!member) {
throw new Error('not authorized');
throw new ServiceException('not authorized', 401);
}
await this.monitorRepository.deleteMonitor(monitor.monitorId);
await this.projectRepository.increaseMonitorCount(monitor.projectId, -1);
Expand All @@ -107,4 +108,57 @@ export class MonitorService {
).deleteAllMonitorAddress(monitor.monitorId);
return Builder<DeleteMonitorResponseDto>().success(true).build();
}

async updateMonitor(
user: User,
request: UpdateMonitorDto,
): Promise<MonitorResponseDto> {
const monitor = await this.monitorRepository.findById(request.monitorId);
if (!monitor) {
throw new ServiceException('monitor not found', 404);
}
const member = await this.projectMemberRepository.findByUserAndProject(
user.userId,
monitor.projectId,
);
if (!member) {
throw new ServiceException('not authorized', 401);
}
const updateMonitor = new Map<string, any>();
if (request.condition) {
if (request.condition.native != undefined) {
updateMonitor['condition.native'] = request.condition.native;
}
if (request.condition.internal != undefined) {
updateMonitor['condition.internal'] = request.condition.internal;
}
if (request.condition.erc721 != undefined) {
updateMonitor['condition.erc721'] = request.condition.erc721;
}
if (request.condition.erc20 != undefined) {
updateMonitor['condition.erc20'] = request.condition.erc20;
}
if (request.condition.specific != undefined) {
updateMonitor['condition.specific'] = request.condition.specific;
}
if (request.condition.cryptos != undefined) {
updateMonitor['condition.cryptos'] = request.condition.cryptos;
}
}
if (request.notification) {
updateMonitor['notification'] = request.notification;
}
if (request.type) {
updateMonitor['type'] = request.type;
}
if (request.note) {
updateMonitor['note'] = request.note;
}
if (request.tags) {
updateMonitor['tags'] = request.tags;
}
return this.monitorRepository
.updateMonitor(monitor.monitorId, updateMonitor)
.then((monitor) => MonitorResponseDto.from(monitor));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,23 @@ export class MonitorRepository {
.deleteOne({ monitorId: monitorId })
.then((result) => result.deletedCount > 0);
}

async updateMonitor(
monitorId: string,
updateMonitor: Map<string, any>,
): Promise<Monitor> {
return this.monitorModel
.findOneAndUpdate(
{ monitorId: monitorId },
{
$set: {
...updateMonitor,
},
},
{
new: true,
},
)
.exec();
}
}

0 comments on commit 6a542e1

Please sign in to comment.