2016-12-28 23:49:51 +01:00
|
|
|
import rndstr from 'rndstr';
|
2017-03-08 19:50:09 +01:00
|
|
|
import $ from 'cafy';
|
2018-03-29 13:32:18 +02:00
|
|
|
import App, { isValidNameId, pack } from '../../../../models/app';
|
2018-06-18 02:54:53 +02:00
|
|
|
import { ILocalUser } from '../../../../models/user';
|
2016-12-28 23:49:51 +01:00
|
|
|
|
2018-07-16 21:36:44 +02:00
|
|
|
export const meta = {
|
2018-08-13 05:30:32 +02:00
|
|
|
requireCredential: false
|
2018-07-16 21:36:44 +02:00
|
|
|
};
|
|
|
|
|
2016-12-28 23:49:51 +01:00
|
|
|
/**
|
|
|
|
* Create an app
|
|
|
|
*/
|
2018-07-05 19:58:29 +02:00
|
|
|
export default async (params: any, user: ILocalUser) => new Promise(async (res, rej) => {
|
2018-03-29 07:48:47 +02:00
|
|
|
// Get 'nameId' parameter
|
2018-05-02 11:06:16 +02:00
|
|
|
const [nameId, nameIdErr] = $.str.pipe(isValidNameId).get(params.nameId);
|
2018-03-29 07:48:47 +02:00
|
|
|
if (nameIdErr) return rej('invalid nameId param');
|
2016-12-28 23:49:51 +01:00
|
|
|
|
|
|
|
// Get 'name' parameter
|
2018-05-02 11:06:16 +02:00
|
|
|
const [name, nameErr] = $.str.get(params.name);
|
2017-03-03 11:48:00 +01:00
|
|
|
if (nameErr) return rej('invalid name param');
|
2016-12-28 23:49:51 +01:00
|
|
|
|
|
|
|
// Get 'description' parameter
|
2018-05-02 11:06:16 +02:00
|
|
|
const [description, descriptionErr] = $.str.get(params.description);
|
2017-03-03 11:48:00 +01:00
|
|
|
if (descriptionErr) return rej('invalid description param');
|
2016-12-28 23:49:51 +01:00
|
|
|
|
|
|
|
// Get 'permission' parameter
|
2018-05-02 11:06:16 +02:00
|
|
|
const [permission, permissionErr] = $.arr($.str).unique().get(params.permission);
|
2017-03-03 11:48:00 +01:00
|
|
|
if (permissionErr) return rej('invalid permission param');
|
2016-12-28 23:49:51 +01:00
|
|
|
|
2018-03-29 07:48:47 +02:00
|
|
|
// Get 'callbackUrl' parameter
|
2017-11-18 17:47:50 +01:00
|
|
|
// TODO: Check it is valid url
|
2018-07-05 16:36:07 +02:00
|
|
|
const [callbackUrl = null, callbackUrlErr] = $.str.optional.nullable.get(params.callbackUrl);
|
2018-03-29 07:48:47 +02:00
|
|
|
if (callbackUrlErr) return rej('invalid callbackUrl param');
|
2016-12-28 23:49:51 +01:00
|
|
|
|
|
|
|
// Generate secret
|
|
|
|
const secret = rndstr('a-zA-Z0-9', 32);
|
|
|
|
|
|
|
|
// Create account
|
2017-01-20 09:38:05 +01:00
|
|
|
const app = await App.insert({
|
2018-03-29 07:48:47 +02:00
|
|
|
createdAt: new Date(),
|
2018-08-13 05:30:32 +02:00
|
|
|
userId: user && user._id,
|
2016-12-28 23:49:51 +01:00
|
|
|
name: name,
|
2018-03-29 07:48:47 +02:00
|
|
|
nameId: nameId,
|
|
|
|
nameIdLower: nameId.toLowerCase(),
|
2016-12-28 23:49:51 +01:00
|
|
|
description: description,
|
2017-03-03 11:48:00 +01:00
|
|
|
permission: permission,
|
2018-03-29 07:48:47 +02:00
|
|
|
callbackUrl: callbackUrl,
|
2016-12-28 23:49:51 +01:00
|
|
|
secret: secret
|
|
|
|
});
|
|
|
|
|
|
|
|
// Response
|
2018-02-02 00:21:30 +01:00
|
|
|
res(await pack(app));
|
2016-12-28 23:49:51 +01:00
|
|
|
});
|