seepur/app/Middleware/CallContext.js

28 lines
764 B
JavaScript

'use strict'
/** @typedef {import('@adonisjs/framework/src/Request')} Request */
/** @typedef {import('@adonisjs/framework/src/Response')} Response */
/** @typedef {import('@adonisjs/framework/src/View')} View */
const Call = use('App/Models/Call');
class CallContext {
/**
* @param {object} ctx
* @param {Request} ctx.request
* @param {Function} next
*/
async handle(ctx, next) {
const {request, auth, response} = ctx;
const callId = request.params.callId;
const call = await Call.find(callId);
if (!call) {
response.status(404);
response.send({code: 404, message: 'Call not found'});
return;
}
ctx.call = call;
// call next to advance the request
await next()
}
}
module.exports = CallContext