summaryrefslogtreecommitdiff
path: root/src/api/endpoints/app/show.ts
blob: 054aab8596d5248d61b29b977e142e2fe4099aba (plain)
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
/**
 * Module dependencies
 */
import $ from 'cafy';
import App from '../../models/app';
import serialize from '../../serializers/app';

/**
 * @swagger
 * /app/show:
 *   post:
 *     summary: Show an application's information
 *     description: Require app_id or name_id
 *     parameters:
 *       -
 *         name: app_id
 *         description: Application ID
 *         in: formData
 *         type: string
 *       -
 *         name: name_id
 *         description: Application unique name
 *         in: formData
 *         type: string
 *
 *     responses:
 *       200:
 *         description: Success
 *         schema:
 *           $ref: "#/definitions/Application"
 *
 *       default:
 *         description: Failed
 *         schema:
 *           $ref: "#/definitions/Error"
 */

/**
 * Show an app
 *
 * @param {any} params
 * @param {any} user
 * @param {any} _
 * @param {any} isSecure
 * @return {Promise<any>}
 */
module.exports = (params, user, _, isSecure) => new Promise(async (res, rej) => {
	// Get 'app_id' parameter
	const [appId, appIdErr] = $(params.app_id).optional.id().$;
	if (appIdErr) return rej('invalid app_id param');

	// Get 'name_id' parameter
	const [nameId, nameIdErr] = $(params.name_id).optional.string().$;
	if (nameIdErr) return rej('invalid name_id param');

	if (appId === undefined && nameId === undefined) {
		return rej('app_id or name_id is required');
	}

	// Lookup app
	const app = appId !== undefined
		? await App.findOne({ _id: appId })
		: await App.findOne({ name_id_lower: nameId.toLowerCase() });

	if (app === null) {
		return rej('app not found');
	}

	// Send response
	res(await serialize(app, user, {
		includeSecret: isSecure && app.user_id.equals(user._id)
	}));
});