2016-12-29 07:49:51 +09:00
|
|
|
/**
|
|
|
|
* Module dependencies
|
|
|
|
*/
|
2017-03-09 03:50:09 +09:00
|
|
|
import $ 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
|
|
|
|
2017-01-06 17:45:04 +09:00
|
|
|
/**
|
|
|
|
* @swagger
|
|
|
|
* /app/name_id/available:
|
|
|
|
* post:
|
|
|
|
* summary: Check available name_id on creation an application
|
|
|
|
* parameters:
|
|
|
|
* -
|
|
|
|
* name: name_id
|
|
|
|
* description: Application unique name
|
|
|
|
* in: formData
|
|
|
|
* required: true
|
|
|
|
* type: string
|
2017-03-01 17:37:01 +09:00
|
|
|
*
|
2017-01-06 17:45:04 +09:00
|
|
|
* responses:
|
|
|
|
* 200:
|
|
|
|
* description: Success
|
|
|
|
* schema:
|
|
|
|
* type: object
|
|
|
|
* properties:
|
|
|
|
* available:
|
|
|
|
* description: Whether name_id is available
|
|
|
|
* type: boolean
|
2017-03-01 17:37:01 +09:00
|
|
|
*
|
2017-01-06 17:45:04 +09:00
|
|
|
* default:
|
|
|
|
* description: Failed
|
|
|
|
* schema:
|
|
|
|
* $ref: "#/definitions/Error"
|
|
|
|
*/
|
|
|
|
|
2016-12-29 07:49:51 +09:00
|
|
|
/**
|
|
|
|
* Check available name_id of app
|
|
|
|
*
|
2017-03-01 17:37:01 +09:00
|
|
|
* @param {any} params
|
|
|
|
* @return {Promise<any>}
|
2016-12-29 07:49:51 +09:00
|
|
|
*/
|
2017-03-04 04:28:38 +09:00
|
|
|
module.exports = async (params) => new Promise(async (res, rej) => {
|
2016-12-29 07:49:51 +09:00
|
|
|
// Get 'name_id' parameter
|
2017-03-09 03:50:09 +09:00
|
|
|
const [nameId, nameIdErr] = $(params.name_id).string().pipe(isValidNameId).$;
|
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 exist
|
|
|
|
const exist = await App
|
|
|
|
.count({
|
|
|
|
name_id_lower: nameId.toLowerCase()
|
|
|
|
}, {
|
|
|
|
limit: 1
|
|
|
|
});
|
|
|
|
|
|
|
|
// Reply
|
|
|
|
res({
|
|
|
|
available: exist === 0
|
|
|
|
});
|
|
|
|
});
|