const models = require('../models'); import * as api from '../../client/src/api'; import { EndpointError, EndpointHandler, catchUnhandledErrors } from './types'; export const ArtistDetailsEndpointHandler: EndpointHandler = async (req: any, res: any) => { if (!api.checkArtistDetailsRequest(req)) { const e: EndpointError = { internalMessage: 'Invalid ArtistDetails request: ' + JSON.stringify(req.body), httpStatus: 400 }; throw e; } try { const artists: any[] = await models.Artist.findAll({ where: { id: req.params.id }, include: [models.Tag] }); if (artists.length != 1) { const e: EndpointError = { internalMessage: 'There is no artist with id ' + req.params.id + '.', httpStatus: 400 }; throw e; } let artist = artists[0]; const storeLinks = Array.isArray(artist.storeLinks) ? artist.storeLinks : (artist.storeLinks ? [artist.storeLinks] : []); const response: api.ArtistDetailsResponse = { name: artist.name, tagIds: artist.Tags.map((tag: any) => tag.id), storeLinks: storeLinks, }; await res.send(response); } catch (e) { catchUnhandledErrors(e) } }