forked from getsentry/integration-platform-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathitems.ts
86 lines (78 loc) · 2.22 KB
/
items.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
import express from 'express';
import Item from '../models/Item.model';
import Organization from '../models/Organization.model';
import SentryAPIClient from '../util/SentryAPIClient';
const router = express.Router();
async function addSentryAPIData(
organization: Organization & {items: Item[]}
): Promise<any[]> {
// Create an APIClient to talk to Sentry
const sentry = await SentryAPIClient.create(organization);
return Promise.all(
organization.items.map(async item => {
if (item.sentryId) {
// Use the numerical ID to fetch the short ID
const sentryData = await sentry.get(`/issues/${item.sentryId}/`);
// Replace the numerical ID with the short ID
const shortId = (sentryData || {})?.data?.shortId;
if (shortId) {
item.sentryId = shortId;
}
return item;
}
return item;
})
);
}
router.get('/', async (request, response) => {
const {organization: slug} = request.query;
if (slug) {
const organization = await Organization.findOne({
include: Item,
where: {slug},
});
if (organization) {
return response.send(
organization.items.some(item => !!item.sentryId)
? await addSentryAPIData(organization)
: organization.items
);
}
}
const items = await Item.findAll();
return response.send(items);
});
router.post('/', async (request, response) => {
const {title, description, complexity, column, assigneeId, organizationId} =
request.body;
const item = await Item.create({
title,
description,
complexity,
column,
assigneeId,
organizationId,
});
return response.status(201).send(item);
});
router.put('/:itemId', async (request, response) => {
const {title, description, complexity, column, assigneeId, organizationId} =
request.body;
const item = await Item.update(
{
title,
description,
complexity,
column,
assigneeId,
organizationId,
},
{where: {id: request.params.itemId}}
);
return response.send(item);
});
router.delete('/:itemId', async (request, response) => {
await Item.destroy({where: {id: request.params.itemId}});
return response.sendStatus(204);
});
export default router;