Add codecheck endpoint
This commit is contained in:
parent
7b9fe95cd4
commit
de5a4b9f19
@ -8,6 +8,9 @@ import versionRoute from './version.js';
|
|||||||
import user_route from './user.js';
|
import user_route from './user.js';
|
||||||
import user_schema from './user_schema.js';
|
import user_schema from './user_schema.js';
|
||||||
|
|
||||||
|
import user_codecheck_route from './user_codecheck.js';
|
||||||
|
import user_codecheck_schema from './user_codecheck_schema.js';
|
||||||
|
|
||||||
import products_route from './products.js';
|
import products_route from './products.js';
|
||||||
import products_schema from './products_schema.js';
|
import products_schema from './products_schema.js';
|
||||||
|
|
||||||
@ -28,6 +31,9 @@ Router.use('*', function (req, res, next) {
|
|||||||
Router.route('/user').get(user_route.get).post(user_route.post).patch(user_route.patch).delete(user_route.del);
|
Router.route('/user').get(user_route.get).post(user_route.post).patch(user_route.patch).delete(user_route.del);
|
||||||
Router.route('/user/describe').get(user_schema);
|
Router.route('/user/describe').get(user_schema);
|
||||||
|
|
||||||
|
Router.route('/user/codecheck').get(user_codecheck_route.get);
|
||||||
|
Router.route('/user/codecheck/describe').get(user_codecheck_schema);
|
||||||
|
|
||||||
Router.route('/products').get(products_route.get).post(products_route.post).patch(products_route.patch).delete(products_route.del);
|
Router.route('/products').get(products_route.get).post(products_route.post).patch(products_route.patch).delete(products_route.del);
|
||||||
Router.route('/products/describe').get(products_schema);
|
Router.route('/products/describe').get(products_schema);
|
||||||
|
|
||||||
|
34
src/routes/api/v1/user_codecheck.ts
Normal file
34
src/routes/api/v1/user_codecheck.ts
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
import { Request, Response } from 'express';
|
||||||
|
import db, { handlePrismaError } from '../../../handlers/db.js'; // Database
|
||||||
|
import log from '../../../handlers/log.js';
|
||||||
|
import { schema_get } from './user_codecheck_schema.js';
|
||||||
|
|
||||||
|
// MARK: GET check user code
|
||||||
|
async function get(req: Request, res: Response) {
|
||||||
|
const { error, value } = schema_get.validate(req.query);
|
||||||
|
if (error) {
|
||||||
|
log.api?.debug('GET check user code Error:', req.query, value, error.details[0].message);
|
||||||
|
res.status(400).json({ status: 'ERROR', errorcode: 'VALIDATION_ERROR', message: error.details[0].message });
|
||||||
|
} else {
|
||||||
|
log.api?.debug('GET check user code Success:', req.query, value);
|
||||||
|
|
||||||
|
await db.user
|
||||||
|
.findUnique({
|
||||||
|
where: {
|
||||||
|
id: value.id
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
handlePrismaError(err, res, 'GET check user code');
|
||||||
|
})
|
||||||
|
.then((result) => {
|
||||||
|
// user has no code OR code must match
|
||||||
|
res.status(200).json(result?.code === '' || value.code === result?.code);
|
||||||
|
// log.api?.debug('result', result);
|
||||||
|
});
|
||||||
|
// res.status(200).json({ count, result });
|
||||||
|
// res.status(404).json({ status: 'ERROR', errorcode: 'NOT_FOUND', message: 'Could not find specified object' });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default { get };
|
32
src/routes/api/v1/user_codecheck_schema.ts
Normal file
32
src/routes/api/v1/user_codecheck_schema.ts
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
import { Request, Response } from 'express';
|
||||||
|
import validator from 'joi'; // DOCS: https://joi.dev/api
|
||||||
|
|
||||||
|
// MARK: GET check user code
|
||||||
|
const schema_get = validator.object({
|
||||||
|
id: validator.number().positive().precision(0).required(),
|
||||||
|
code: validator
|
||||||
|
.string()
|
||||||
|
.min(4)
|
||||||
|
.max(4)
|
||||||
|
.trim()
|
||||||
|
.regex(new RegExp(/^[0-9]+$/))
|
||||||
|
.required()
|
||||||
|
});
|
||||||
|
|
||||||
|
// Describe all schemas
|
||||||
|
const schema_get_desc = schema_get.describe();
|
||||||
|
// const schema_post_desc = schema_post.describe();
|
||||||
|
// const schema_patch_desc = schema_patch.describe();
|
||||||
|
// const schema_del_desc = schema_del.describe();
|
||||||
|
|
||||||
|
// GET route
|
||||||
|
export default async function get(req: Request, res: Response) {
|
||||||
|
res.status(200).json({
|
||||||
|
GET: schema_get_desc
|
||||||
|
// POST: schema_post_desc,
|
||||||
|
// PATCH: schema_patch_desc,
|
||||||
|
// DELETE: schema_del_desc
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export { schema_get }; //, schema_post, schema_patch, schema_del };
|
Loading…
x
Reference in New Issue
Block a user