2016-12-29 07:49:51 +09:00
|
|
|
/**
|
|
|
|
* Module dependencies
|
|
|
|
*/
|
|
|
|
import rndstr from 'rndstr';
|
2017-03-05 12:00:39 +09:00
|
|
|
import it from 'cafy';
|
2016-12-29 07:49:51 +09:00
|
|
|
import App from '../../models/app';
|
2017-03-03 19:48:00 +09:00
|
|
|
import { isValidNameId } from '../../models/app';
|
2016-12-29 07:49:51 +09:00
|
|
|
import serialize from '../../serializers/app';
|
|
|
|
|
2017-01-06 18:33:03 +09:00
|
|
|
/**
|
|
|
|
* @swagger
|
|
|
|
* /app/create:
|
|
|
|
* post:
|
|
|
|
* summary: Create an application
|
|
|
|
* parameters:
|
|
|
|
* - $ref: "#/parameters/AccessToken"
|
|
|
|
* -
|
|
|
|
* name: name_id
|
|
|
|
* 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
|
|
|
|
* -
|
|
|
|
* name: callback_url
|
|
|
|
* description: URL called back after authentication
|
|
|
|
* in: formData
|
|
|
|
* required: false
|
|
|
|
* type: string
|
2017-03-01 17:37:01 +09:00
|
|
|
*
|
2017-01-06 18:33:03 +09:00
|
|
|
* responses:
|
|
|
|
* 200:
|
|
|
|
* description: Created application's information
|
|
|
|
* schema:
|
|
|
|
* $ref: "#/definitions/Application"
|
2017-03-01 17:37:01 +09:00
|
|
|
*
|
2017-01-06 18:33:03 +09:00
|
|
|
* default:
|
|
|
|
* description: Failed
|
|
|
|
* schema:
|
|
|
|
* $ref: "#/definitions/Error"
|
|
|
|
*/
|
|
|
|
|
2016-12-29 07:49:51 +09:00
|
|
|
/**
|
|
|
|
* Create an app
|
|
|
|
*
|
2017-03-01 17:37:01 +09:00
|
|
|
* @param {any} params
|
|
|
|
* @param {any} user
|
|
|
|
* @return {Promise<any>}
|
2016-12-29 07:49:51 +09:00
|
|
|
*/
|
2017-03-04 04:28:38 +09:00
|
|
|
module.exports = async (params, user) => new Promise(async (res, rej) => {
|
2016-12-29 07:49:51 +09:00
|
|
|
// Get 'name_id' parameter
|
2017-03-05 12:00:39 +09:00
|
|
|
const [nameId, nameIdErr] = it(params.name_id).expect.string().required().validate(isValidNameId).get();
|
2017-03-03 19:48:00 +09:00
|
|
|
if (nameIdErr) return rej('invalid name_id param');
|
2016-12-29 07:49:51 +09:00
|
|
|
|
|
|
|
// Get 'name' parameter
|
2017-03-05 12:00:39 +09:00
|
|
|
const [name, nameErr] = it(params.name).expect.string().required().get();
|
2017-03-03 19:48:00 +09:00
|
|
|
if (nameErr) return rej('invalid name param');
|
2016-12-29 07:49:51 +09:00
|
|
|
|
|
|
|
// Get 'description' parameter
|
2017-03-05 12:00:39 +09:00
|
|
|
const [description, descriptionErr] = it(params.description).expect.string().required().get();
|
2017-03-03 19:48:00 +09:00
|
|
|
if (descriptionErr) return rej('invalid description param');
|
2016-12-29 07:49:51 +09:00
|
|
|
|
|
|
|
// Get 'permission' parameter
|
2017-03-05 12:00:39 +09:00
|
|
|
const [permission, permissionErr] = it(params.permission).expect.array().unique().allString().required().get();
|
2017-03-03 19:48:00 +09:00
|
|
|
if (permissionErr) return rej('invalid permission param');
|
2016-12-29 07:49:51 +09:00
|
|
|
|
|
|
|
// Get 'callback_url' parameter
|
2017-03-03 19:48:00 +09:00
|
|
|
// TODO: Check it is valid url
|
2017-03-05 12:00:39 +09:00
|
|
|
const [callbackUrl = null, callbackUrlErr] = it(params.callback_url).expect.nullable.string().get();
|
2017-03-03 19:48:00 +09:00
|
|
|
if (callbackUrlErr) return rej('invalid callback_url param');
|
2016-12-29 07:49:51 +09:00
|
|
|
|
|
|
|
// Generate secret
|
|
|
|
const secret = rndstr('a-zA-Z0-9', 32);
|
|
|
|
|
|
|
|
// Create account
|
2017-01-20 17:38:05 +09:00
|
|
|
const app = await App.insert({
|
2016-12-29 07:49:51 +09:00
|
|
|
created_at: new Date(),
|
|
|
|
user_id: user._id,
|
|
|
|
name: name,
|
|
|
|
name_id: nameId,
|
|
|
|
name_id_lower: nameId.toLowerCase(),
|
|
|
|
description: description,
|
2017-03-03 19:48:00 +09:00
|
|
|
permission: permission,
|
|
|
|
callback_url: callbackUrl,
|
2016-12-29 07:49:51 +09:00
|
|
|
secret: secret
|
|
|
|
});
|
|
|
|
|
|
|
|
// Response
|
|
|
|
res(await serialize(app));
|
|
|
|
});
|