jormungandr-bite/src/server/api/endpoints/app/name_id/available.ts

61 lines
1.3 KiB
TypeScript
Raw Normal View History

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