'use strict'; const knex = require('./knex'); const whereOr = require('./utils/where-or'); async function curateActor(actor) { const aliases = await knex('actors') .where({ alias_for: actor.id }); return { id: actor.id, name: actor.name, description: actor.description, birthdate: actor.birthdate && new Date(actor.birthdate), country: actor.country_alpha2, city: actor.city, ethnicity: actor.ethnicity, height: actor.height, boobSize: actor.boobs_size, boobsNatural: actor.boobs_natural, aliases: aliases.map(({ name }) => name), slug: actor.slug, }; } function curateActors(releases) { return Promise.all(releases.map(async release => curateActor(release))); } async function fetchActors(queryObject) { const releases = await knex('actors') .where(builder => whereOr(queryObject, 'actors', builder)) .limit(100); return curateActors(releases); } async function storeActors(release, releaseEntry) { const actors = await knex('actors').whereIn('name', release.actors); const newActors = release.actors.filter(actorName => !actors.some(actor => actor.name === actorName)); const { rows: insertedActors } = newActors.length ? await knex.raw(`${knex('actors').insert(newActors.map(actorName => ({ name: actorName, slug: actorName.toLowerCase().replace(/\s+/g, '-'), })))} ON CONFLICT DO NOTHING RETURNING *`) : { rows: [] }; return knex('actors_associated').insert(actors.concat(insertedActors).map(actor => ({ release_id: releaseEntry.id, actor_id: actor.id, })), '*'); } module.exports = { fetchActors, storeActors, };