2016-12-28 16:49:51 -06:00
|
|
|
/**
|
|
|
|
* Module dependencies
|
|
|
|
*/
|
|
|
|
import rndstr from 'rndstr';
|
2017-03-08 12:50:09 -06:00
|
|
|
import $ from 'cafy';
|
2018-03-29 06:32:18 -05:00
|
|
|
import App, { isValidNameId, pack } from '../../../../models/app';
|
2016-12-28 16:49:51 -06:00
|
|
|
|
2017-01-06 03:33:03 -06:00
|
|
|
/**
|
|
|
|
* @swagger
|
|
|
|
* /app/create:
|
2018-04-07 12:30:37 -05:00
|
|
|
* note:
|
2017-01-06 03:33:03 -06:00
|
|
|
* summary: Create an application
|
|
|
|
* parameters:
|
|
|
|
* - $ref: "#/parameters/AccessToken"
|
|
|
|
* -
|
2018-03-29 00:48:47 -05:00
|
|
|
* name: nameId
|
2017-01-06 03:33:03 -06:00
|
|
|
* description: Application unique name
|
|
|
|
* in: formData
|
|
|
|
* required: true
|
|
|
|
* type: string
|
|
|
|
* -
|
|
|
|
* name: name
|
|
|
|
* description: Application name
|
|
|
|
* in: formData
|
|
|
|
* required: true
|
|
|
|
* type: string
|
|
|
|
* -
|
|
|
|
* name: description
|
|
|
|
* description: Application description
|
|
|
|
* in: formData
|
|
|
|
* required: true
|
|
|
|
* type: string
|
|
|
|
* -
|
|
|
|
* name: permission
|
|
|
|
* description: Permissions that application has
|
|
|
|
* in: formData
|
|
|
|
* required: true
|
|
|
|
* type: array
|
|
|
|
* items:
|
|
|
|
* type: string
|
|
|
|
* collectionFormat: csv
|
|
|
|
* -
|
2018-03-29 00:48:47 -05:00
|
|
|
* name: callbackUrl
|
2017-01-06 03:33:03 -06:00
|
|
|
* description: URL called back after authentication
|
|
|
|
* in: formData
|
|
|
|
* required: false
|
|
|
|
* type: string
|
2017-03-01 02:37:01 -06:00
|
|
|
*
|
2017-01-06 03:33:03 -06:00
|
|
|
* responses:
|
|
|
|
* 200:
|
|
|
|
* description: Created application's information
|
|
|
|
* schema:
|
|
|
|
* $ref: "#/definitions/Application"
|
2017-03-01 02:37:01 -06:00
|
|
|
*
|
2017-01-06 03:33:03 -06:00
|
|
|
* default:
|
|
|
|
* description: Failed
|
|
|
|
* schema:
|
|
|
|
* $ref: "#/definitions/Error"
|
|
|
|
*/
|
|
|
|
|
2016-12-28 16:49:51 -06:00
|
|
|
/**
|
|
|
|
* Create an app
|
|
|
|
*
|
2017-03-01 02:37:01 -06:00
|
|
|
* @param {any} params
|
|
|
|
* @param {any} user
|
|
|
|
* @return {Promise<any>}
|
2016-12-28 16:49:51 -06:00
|
|
|
*/
|
2017-03-03 13:28:38 -06:00
|
|
|
module.exports = async (params, user) => new Promise(async (res, rej) => {
|
2018-03-29 00:48:47 -05:00
|
|
|
// Get 'nameId' parameter
|
|
|
|
const [nameId, nameIdErr] = $(params.nameId).string().pipe(isValidNameId).$;
|
|
|
|
if (nameIdErr) return rej('invalid nameId param');
|
2016-12-28 16:49:51 -06:00
|
|
|
|
|
|
|
// Get 'name' parameter
|
2017-03-08 12:50:09 -06:00
|
|
|
const [name, nameErr] = $(params.name).string().$;
|
2017-03-03 04:48:00 -06:00
|
|
|
if (nameErr) return rej('invalid name param');
|
2016-12-28 16:49:51 -06:00
|
|
|
|
|
|
|
// Get 'description' parameter
|
2017-03-08 12:50:09 -06:00
|
|
|
const [description, descriptionErr] = $(params.description).string().$;
|
2017-03-03 04:48:00 -06:00
|
|
|
if (descriptionErr) return rej('invalid description param');
|
2016-12-28 16:49:51 -06:00
|
|
|
|
|
|
|
// Get 'permission' parameter
|
2018-04-24 04:13:06 -05:00
|
|
|
const [permission, permissionErr] = $(params.permission).array($().string()).unique().$;
|
2017-03-03 04:48:00 -06:00
|
|
|
if (permissionErr) return rej('invalid permission param');
|
2016-12-28 16:49:51 -06:00
|
|
|
|
2018-03-29 00:48:47 -05:00
|
|
|
// Get 'callbackUrl' parameter
|
2017-11-18 10:47:50 -06:00
|
|
|
// TODO: Check it is valid url
|
2018-03-29 00:48:47 -05:00
|
|
|
const [callbackUrl = null, callbackUrlErr] = $(params.callbackUrl).optional.nullable.string().$;
|
|
|
|
if (callbackUrlErr) return rej('invalid callbackUrl param');
|
2016-12-28 16:49:51 -06:00
|
|
|
|
|
|
|
// Generate secret
|
|
|
|
const secret = rndstr('a-zA-Z0-9', 32);
|
|
|
|
|
|
|
|
// Create account
|
2017-01-20 02:38:05 -06:00
|
|
|
const app = await App.insert({
|
2018-03-29 00:48:47 -05:00
|
|
|
createdAt: new Date(),
|
|
|
|
userId: user._id,
|
2016-12-28 16:49:51 -06:00
|
|
|
name: name,
|
2018-03-29 00:48:47 -05:00
|
|
|
nameId: nameId,
|
|
|
|
nameIdLower: nameId.toLowerCase(),
|
2016-12-28 16:49:51 -06:00
|
|
|
description: description,
|
2017-03-03 04:48:00 -06:00
|
|
|
permission: permission,
|
2018-03-29 00:48:47 -05:00
|
|
|
callbackUrl: callbackUrl,
|
2016-12-28 16:49:51 -06:00
|
|
|
secret: secret
|
|
|
|
});
|
|
|
|
|
|
|
|
// Response
|
2018-02-01 17:21:30 -06:00
|
|
|
res(await pack(app));
|
2016-12-28 16:49:51 -06:00
|
|
|
});
|