-
-
Notifications
You must be signed in to change notification settings - Fork 130
/
push.controller.ts
115 lines (109 loc) · 4.01 KB
/
push.controller.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import { Body, Controller, Delete, HttpStatus, Param, Post, Put } from '@nestjs/common';
import { ApiOAuth2Auth, ApiOperation, ApiResponse, ApiUseTags } from '@nestjs/swagger';
import { CrudController } from '../core';
import { CurrentUser, User } from '../auth';
import { Subscription } from './subscription.entity';
import { PushService } from './push.service';
import { CreateSubscriptionDto } from './dto/create-subscription.dto';
import { UpdateSubscriptionDto } from './dto/update-subscription.dto';
import { SendNotificationDto } from './dto/send-notification.dto';
import { SendTopicNotificationDto } from './dto/send-topic-notification.dto';
@ApiOAuth2Auth(['read'])
@ApiUseTags('Sumo', 'Push')
@Controller()
export class PushController extends CrudController<Subscription> {
constructor(private readonly pushService: PushService) {
super(pushService);
}
@ApiOperation({ title: 'Create new record' })
@ApiResponse({
status: HttpStatus.CREATED,
description: 'The record has been successfully created.',
type: Subscription,
})
@ApiResponse({
status: HttpStatus.BAD_REQUEST,
description: 'Invalid input, The response body may contain clues as to what went wrong',
})
@Post()
async create(@Body() entity: CreateSubscriptionDto, @CurrentUser() user: User): Promise<Subscription> {
const records = await this.pushService.findAndCount({ endpoint: entity.endpoint });
if (records[1] === 0) {
return super.create({ ...entity, userId: user.userId });
} else {
return records[0][0];
}
}
@ApiOperation({ title: 'Update an existing record' })
@ApiResponse({ status: HttpStatus.CREATED, description: 'The record has been successfully edited.' })
@ApiResponse({ status: HttpStatus.NOT_FOUND, description: 'Record not found' })
@ApiResponse({
status: HttpStatus.BAD_REQUEST,
description: 'Invalid input, The response body may contain clues as to what went wrong',
})
@Put(':id')
async update(
@Param('id') id: string,
@Body() entity: UpdateSubscriptionDto,
@CurrentUser() user: User,
): Promise<any> {
if (id.startsWith('http')) {
return this.pushService.update({ endpoint: id }, entity);
} else {
return this.pushService.update(id, entity);
}
}
@ApiOperation({ title: 'Delete record' })
@ApiResponse({ status: HttpStatus.NO_CONTENT, description: 'The record has been successfully deleted' })
@ApiResponse({ status: HttpStatus.NOT_FOUND, description: 'Record not found' })
@Delete(':id')
async delete(@Param('id') id: string, @CurrentUser() user: User): Promise<any> {
if (id.startsWith('http')) {
return this.pushService.delete({ endpoint: id });
} else {
return this.pushService.delete(id);
}
}
@ApiOperation({ title: 'Send Push Notification' })
@ApiResponse({
status: HttpStatus.CREATED,
description: 'Push Notification has been successfully sent.',
})
@ApiResponse({
status: HttpStatus.BAD_REQUEST,
description: 'Invalid input, The response body may contain clues as to what went wrong',
})
@Post('notify')
notify(@Body() notif: SendNotificationDto) {
const notification = {
title: notif.title,
body: notif.body,
icon: 'assets/icons/icon-72x72.png',
data: {
click_url: '/dashboard',
},
};
return this.pushService.notify(notif.id, notification as any);
}
@ApiOperation({ title: 'Send Push Notifications to all subscribers to a topic' })
@ApiResponse({
status: HttpStatus.CREATED,
description: 'Push Notifications has been successfully sent.',
})
@ApiResponse({
status: HttpStatus.BAD_REQUEST,
description: 'Invalid input, The response body may contain clues as to what went wrong',
})
@Post('notifyAll')
notifyAll(@Body() notif: SendTopicNotificationDto) {
const notification = {
title: notif.title,
body: notif.body,
icon: 'assets/icons/icon-72x72.png',
data: {
click_url: '/dashboard',
},
};
return this.pushService.notifyAll(notif.topic, notification as any);
}
}