const models = require('../models'); import * as api from '../../client/src/api'; import { EndpointError, EndpointHandler, catchUnhandledErrors } from './types'; const { Op } = require("sequelize"); export const CreateArtistEndpointHandler: EndpointHandler = async (req: any, res: any) => { if (!api.checkCreateArtistRequest(req)) { const e: EndpointError = { internalMessage: 'Invalid CreateArtist request: ' + JSON.stringify(req.body), httpStatus: 400 }; throw e; } const reqObject: api.CreateArtistRequest = req.body; console.log("Create artist:", reqObject) try { // Start retrieving the tag instances to link the artist to. const tags = reqObject.tagIds && await models.Tag.findAll({ where: { id: { [Op.in]: reqObject.tagIds } } }); console.log("Found artist tags:", tags) if (reqObject.tagIds && tags.length !== reqObject.tagIds.length) { const e: EndpointError = { internalMessage: 'Not all tags exist for CreateArtist request: ' + JSON.stringify(req.body), httpStatus: 400 }; throw e; } var artist = models.Artist.build({ name: reqObject.name, storeLinks: reqObject.storeLinks || [], }); tags && artist.addTags(tags); await artist.save(); const responseObject: api.CreateSongResponse = { id: artist.id }; await res.status(200).send(responseObject); } catch (e) { catchUnhandledErrors(e); } }