import * as api from '../../client/src/api'; import { EndpointError, EndpointHandler, catchUnhandledErrors } from './types'; import Knex from 'knex'; import asJson from '../lib/asJson'; export const ArtistDetailsEndpointHandler: EndpointHandler = async (req: any, res: any, knex: Knex) => { if (!api.checkArtistDetailsRequest(req)) { const e: EndpointError = { internalMessage: 'Invalid ArtistDetails request: ' + JSON.stringify(req.body), httpStatus: 400 }; throw e; } const { id: userId } = req.user; try { const tagIds = Array.from(new Set((await knex.select('tagId') .from('artists_tags') .where({ 'artistId': req.params.id }) ).map((tag: any) => tag['tagId']))); const results = await knex.select(['id', 'name', 'storeLinks']) .from('artists') .where({ 'user': userId }) .where({ 'id': req.params.id }); const response: api.ArtistDetailsResponse = { name: results[0].name, tagIds: tagIds, storeLinks: asJson(results[0].storeLinks), } await res.send(response); } catch (e) { catchUnhandledErrors(e) } }