-
-
Notifications
You must be signed in to change notification settings - Fork 687
/
Copy pathfunction.controller.ts
289 lines (270 loc) · 8.18 KB
/
function.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
import {
Controller,
Get,
Post,
Body,
Patch,
Param,
Delete,
UseGuards,
HttpException,
HttpStatus,
Req,
} from '@nestjs/common'
import { CreateFunctionDto } from './dto/create-function.dto'
import { UpdateFunctionDto } from './dto/update-function.dto'
import {
ApiResponseArray,
ApiResponseObject,
ResponseUtil,
} from '../utils/response'
import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger'
import { FunctionService } from './function.service'
import { IRequest } from '../utils/interface'
import { CompileFunctionDto } from './dto/compile-function.dto'
import { BundleService } from 'src/application/bundle.service'
import { I18n, I18nContext, I18nService } from 'nestjs-i18n'
import { I18nTranslations } from '../generated/i18n.generated'
import { JwtAuthGuard } from 'src/authentication/jwt.auth.guard'
import { ApplicationAuthGuard } from 'src/authentication/application.auth.guard'
import { CloudFunctionHistory } from './entities/cloud-function-history'
import { CloudFunction } from './entities/cloud-function'
import { UpdateFunctionDebugDto } from './dto/update-function-debug.dto'
import { FunctionRecycleBinService } from 'src/recycle-bin/cloud-function/function-recycle-bin.service'
import { STORAGE_LIMIT } from 'src/constants'
@ApiTags('Function')
@ApiBearerAuth('Authorization')
@Controller('apps/:appid/functions')
export class FunctionController {
constructor(
private readonly functionsService: FunctionService,
private readonly bundleService: BundleService,
private readonly functionRecycleBinService: FunctionRecycleBinService,
private readonly i18n: I18nService<I18nTranslations>,
) {}
/**
* Create a new function
* @param dto
* @returns
*/
@ApiResponseObject(CloudFunction)
@ApiOperation({ summary: 'Create a new function' })
@UseGuards(JwtAuthGuard, ApplicationAuthGuard)
@Post()
async create(
@Param('appid') appid: string,
@Body() dto: CreateFunctionDto,
@Req() req: IRequest,
@I18n() i18n: I18nContext<I18nTranslations>,
) {
const error = dto.validate()
if (error) {
return ResponseUtil.error(error)
}
// check name is unique
const found = await this.functionsService.findOne(appid, dto.name)
if (found) {
return ResponseUtil.error(
i18n.t('function.create.nameExist', { args: { name: dto.name } }),
)
}
// check if meet the count limit
const bundle = await this.bundleService.findOne(appid)
const MAX_FUNCTION_COUNT = bundle?.resource?.limitCountOfCloudFunction || 0
const count = await this.functionsService.count(appid)
if (count >= MAX_FUNCTION_COUNT) {
return ResponseUtil.error(
i18n.t('function.create.maxCount', {
args: { count: MAX_FUNCTION_COUNT },
}),
)
}
const res = await this.functionsService.create(appid, req.user._id, dto)
if (!res) {
return ResponseUtil.error(i18n.t('function.create.error'))
}
return ResponseUtil.ok(res)
}
/**
* Query function list of an app
* @returns
*/
@ApiResponseArray(CloudFunction)
@ApiOperation({ summary: 'Query function list of an app' })
@UseGuards(JwtAuthGuard, ApplicationAuthGuard)
@Get()
async findAll(@Param('appid') appid: string) {
const data = await this.functionsService.findAll(appid)
return ResponseUtil.ok(data)
}
/**
* Get a function by its name
* @param appid
* @param name
*/
@ApiResponseObject(CloudFunction)
@ApiOperation({ summary: 'Get a function by its name' })
@UseGuards(JwtAuthGuard, ApplicationAuthGuard)
@Get(':name')
async findOne(
@Param('appid') appid: string,
@Param('name') name: string,
@I18n() i18n: I18nContext<I18nTranslations>,
) {
const data = await this.functionsService.findOne(appid, name)
if (!data) {
throw new HttpException(
i18n.t('function.common.notFound', { args: { name } }),
HttpStatus.NOT_FOUND,
)
}
return ResponseUtil.ok(data)
}
/**
* Update function debug info
* @param appid
* @param name
* @param dto
* @returns
*/
@ApiResponseObject(CloudFunction)
@ApiOperation({ summary: 'Update function debug info' })
@UseGuards(JwtAuthGuard, ApplicationAuthGuard)
@Patch(':name/debug/params')
async updateDebug(
@Param('appid') appid: string,
@Param('name') name: string,
@Body() dto: UpdateFunctionDebugDto,
@I18n() i18n: I18nContext<I18nTranslations>,
) {
const func = await this.functionsService.findOne(appid, name)
if (!func) {
throw new HttpException(
i18n.t('function.common.notFound', { args: { name } }),
HttpStatus.NOT_FOUND,
)
}
const res = await this.functionsService.updateOneDebug(func, dto)
if (!res) {
return ResponseUtil.error(i18n.t('function.update.error'))
}
return ResponseUtil.ok(res)
}
/**
* Update a function
* @param appid
* @param name
* @param dto
* @returns
*/
@ApiResponseObject(CloudFunction)
@ApiOperation({ summary: 'Update a function' })
@UseGuards(JwtAuthGuard, ApplicationAuthGuard)
@Patch(':name')
async update(
@Param('appid') appid: string,
@Param('name') name: string,
@Body() dto: UpdateFunctionDto,
@I18n() i18n: I18nContext<I18nTranslations>,
) {
const func = await this.functionsService.findOne(appid, name)
if (!func) {
throw new HttpException(
i18n.t('function.common.notFound', { args: { name } }),
HttpStatus.NOT_FOUND,
)
}
const res = await this.functionsService.updateOne(func, dto)
if (!res) {
return ResponseUtil.error(i18n.t('function.update.error'))
}
if (res instanceof Error) {
return ResponseUtil.error(res.message)
}
return ResponseUtil.ok(res)
}
/**
* Delete a function
* @param appid
* @param name
* @returns
*/
@ApiResponseObject(CloudFunction)
@ApiOperation({ summary: 'Delete a function' })
@UseGuards(JwtAuthGuard, ApplicationAuthGuard)
@Delete(':name')
async remove(
@Param('appid') appid: string,
@Param('name') name: string,
@I18n() i18n: I18nContext<I18nTranslations>,
) {
const func = await this.functionsService.findOne(appid, name)
if (!func) {
throw new HttpException(
i18n.t('function.common.notFound', { args: { name } }),
HttpStatus.NOT_FOUND,
)
}
const recycleBinStorage =
await this.functionRecycleBinService.getRecycleBinStorage(appid)
if (recycleBinStorage >= STORAGE_LIMIT) {
return ResponseUtil.error('Recycle bin is full, please free up space')
}
const res = await this.functionsService.removeOne(func)
if (!res) {
return ResponseUtil.error(i18n.t('function.delete.error'))
}
return ResponseUtil.ok(res)
}
/**
* Compile a function
* @param appid
* @param name
* @returns
*/
@ApiResponseObject(CloudFunction)
@ApiOperation({ summary: 'Compile a function ' })
@UseGuards(JwtAuthGuard, ApplicationAuthGuard)
@Post(':name/compile')
async compile(
@Param('appid') appid: string,
@Param('name') name: string,
@Body() dto: CompileFunctionDto,
@I18n() i18n: I18nContext<I18nTranslations>,
) {
if (!dto.code) {
return ResponseUtil.error(i18n.t('function.compile.codeRequired'))
}
const func = await this.functionsService.findOne(appid, name)
if (!func) {
throw new HttpException(
i18n.t('function.common.notFound', { args: { name } }),
HttpStatus.NOT_FOUND,
)
}
const res = await this.functionsService.compile(func, dto)
return ResponseUtil.ok(res)
}
/**
* Get function history
*/
@ApiResponseArray(CloudFunctionHistory)
@ApiOperation({ summary: 'Get cloud function history' })
@UseGuards(JwtAuthGuard, ApplicationAuthGuard)
@Get(':name/history')
async getHistory(
@Param('appid') appid: string,
@Param('name') name: string,
@I18n() i18n: I18nContext<I18nTranslations>,
) {
const func = await this.functionsService.findOne(appid, name)
if (!func) {
throw new HttpException(
i18n.t('function.common.notFound', { args: { name } }),
HttpStatus.NOT_FOUND,
)
}
const res = await this.functionsService.getHistory(func)
return ResponseUtil.ok(res)
}
}