forked from Clovis-team/clovis-open-code-extracts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi-server:controller:blueprints.js
164 lines (140 loc) · 4.24 KB
/
api-server:controller:blueprints.js
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
const _ = require('lodash')
const validate = require('../validate')
const router = require('koa-router')()
const { Blueprint, Project, Task } = require('../models')
const { idParamHandler } = require('./helpers')
async function requireProjectMembership(ctx, next) {
const project = await Project.findById(ctx.blueprint.project)
if (!await project.getMemberType(ctx.me)) {
ctx.throw(403)
}
await next()
}
const tasksRouter = require('koa-router')()
tasksRouter
/**
* @swagger
* /blueprints/id/{id}/tasks:
* get:
* summary: Returns an array of tasks which are localized on the blueprint.
* tags:
* - blueprint
* parameters:
* - name: id
* in: path
* description: The blueprint id
* required: true
* type: string
* produces:
* - application/json
* responses:
* '200':
* description: successful operation
* schema:
* type: array
* items:
* $ref: '#/definitions/Task'
*/
.get('/', async ctx => {
const tasks = await Task.find({
'locationOnBlueprint.blueprint': ctx.blueprint._id,
})
ctx.body = tasks.map(t => t.export())
})
router
.param('id', idParamHandler(Blueprint))
/**
* @swagger
* /blueprints/id/{id}:
* get:
* summary: Get the blueprint document.
* tags:
* - blueprint
* parameters:
* - name: id
* in: path
* description: The blueprint id
* required: true
* type: string
* produces:
* - application/json
* responses:
* '200':
* description: successful operation
* schema:
* $ref: '#/definitions/Blueprint'
*/
.get('/id/:id', requireProjectMembership, async ctx => {
ctx.body = ctx.blueprint.export()
})
/**
* @swagger
* /blueprints/id/{id}:
* put:
* summary: Update the name of the blueprint.
* tags:
* - blueprint
* parameters:
* - name: id
* in: path
* description: The blueprint id
* required: true
* type: string
* - name: body
* in: body
* required: true
* schema:
* type: object
* properties:
* name:
* type: string
* produces:
* - application/json
* responses:
* '204':
* description: The blueprint was updated successfully
*/
.put('/id/:id', requireProjectMembership, async ctx => {
const schema = {
name: String,
}
const body = validate(
ctx.request.body,
_.mapValues(schema, type => ({ type, optional: true })),
)
await ctx.blueprint.update(body)
ctx.status = 204
})
/**
* @swagger
* /blueprints/id/{id}:
* delete:
* summary: Delete the blueprint in the database and in the S3 bucket.
* tags:
* - blueprint
* parameters:
* - name: id
* in: path
* description: The blueprint id
* required: true
* type: string
* produces:
* - application/json
* responses:
* '204':
* description: The blueprint was deleted successfully
*/
.delete('/id/:id', requireProjectMembership, async ctx => {
const { blueprint } = ctx
const relatedTasks = await Task.find({
'locationOnBlueprint.blueprint': blueprint._id,
})
if (relatedTasks.length > 0) {
ctx.throw('existingRelativeTasks', 409)
}
blueprint.deleted = true
await blueprint.save()
ctx.status = 204
})
.use('/id/:id/tasks', requireProjectMembership, tasksRouter.routes())
module.exports = router.routes()