const models = require('../models'); import * as api from '../../client/src/api'; import { EndpointError, EndpointHandler, catchUnhandledErrors } from './types'; export const TagDetailsEndpointHandler: EndpointHandler = async (req: any, res: any) => { if (!api.checkTagDetailsRequest(req)) { const e: EndpointError = { internalMessage: 'Invalid TagDetails request: ' + JSON.stringify(req.body), httpStatus: 400 }; throw e; } await models.Tag.findAll({ where: { id: req.params.id }, include: [{ model: models.Tag, as: 'parent' }] }) .then((tags: any[]) => { if (tags.length != 1) { const e: EndpointError = { internalMessage: 'There is no tag with id ' + req.params.id + '.', httpStatus: 400 }; throw e; } let tag = tags[0]; var response: api.TagDetailsResponse = { name: tag.name, }; if(tag.parent) { response['parentId'] = tag.parent.id; } res.send(response); }) .catch(catchUnhandledErrors); }