130 lines
4.2 KiB
TypeScript
130 lines
4.2 KiB
TypeScript
|
|
import { enhance } from "@zenstackhq/runtime";
|
|
import { db } from "~/utils/db.server";
|
|
import { DrinkComposite } from "./types";
|
|
import lodash from "lodash"
|
|
import { DrinkType, Manufacturer } from "@prisma/client";
|
|
|
|
export async function findIdFromSlug(slug:string | undefined) : Promise<number | undefined> {
|
|
if(slug){
|
|
const drink = await db.drink.findUnique({where: {slug: slug}, select: {id: true}});
|
|
return drink?.id;
|
|
}
|
|
return undefined;
|
|
}
|
|
|
|
export async function findManufacturersFromSearch(searchParams: URLSearchParams) : Promise<Manufacturer[]> {
|
|
const dbe = enhance(db);
|
|
|
|
var result : Manufacturer[] = [];
|
|
|
|
if(searchParams.has("drink")){
|
|
result = result.concat(await dbe.manufacturer.findMany({
|
|
where: {drinks: { some: { type: { in: searchParams.getAll("drink").map((str) => {return DrinkType[str as keyof typeof DrinkType]})}}}}
|
|
}));
|
|
}
|
|
else {
|
|
result = result.concat(await dbe.manufacturer.findMany());
|
|
}
|
|
|
|
return result;
|
|
}
|
|
export async function findDrinksFromSearch(searchParams: URLSearchParams) : Promise<DrinkComposite[]> {
|
|
const dbe = enhance(db);
|
|
|
|
var result : DrinkComposite[] = [];
|
|
var noDrinkSelected = searchParams.getAll("drink").length == 0;
|
|
|
|
let whereBase = {delegate_aux_drink: {AND: <any>[]}};
|
|
|
|
if(searchParams.has("gluten-free")){
|
|
whereBase.delegate_aux_drink.AND.push({NOT: {gluten: true}});
|
|
}
|
|
if(searchParams.has("lactose-free")){
|
|
whereBase.delegate_aux_drink.AND.push({NOT: {lactose: true}});
|
|
}
|
|
if(searchParams.has("organic")){
|
|
whereBase.delegate_aux_drink.AND.push({organic: true});
|
|
}
|
|
|
|
if(searchParams.has("min-abv")){
|
|
let minabv = Number(searchParams.get("min-abv"));
|
|
whereBase.delegate_aux_drink.AND.push({abv: {gte: minabv}});
|
|
}
|
|
if(searchParams.has("max-abv")){
|
|
let maxabv = Number(searchParams.get("max-abv"));
|
|
whereBase.delegate_aux_drink.AND.push({abv: {lte: maxabv}});
|
|
}
|
|
|
|
if(searchParams.has("manufacturer")){
|
|
let whereManufacturer = {manufacturer: {id: {in: searchParams.getAll("manufacturer").map(Number)}}};
|
|
whereBase = lodash.merge(whereBase, whereManufacturer);
|
|
}
|
|
|
|
if(!searchParams.has("inventory")){
|
|
let whereInventory = {containers: {some: {inventory: {gt: 0}}}};
|
|
whereBase = lodash.merge(whereBase, whereInventory);
|
|
}
|
|
|
|
// Beers
|
|
if(searchParams.has("drink", "Beer") || noDrinkSelected){
|
|
|
|
let where = Object.assign({}, whereBase, {style: {}});
|
|
|
|
// Beer styles
|
|
if(searchParams.has("beerstyle")){
|
|
where.style = {id: {in: searchParams.getAll("beerstyle").map(Number)}};
|
|
}
|
|
|
|
result = result.concat(await dbe.beer.findMany({
|
|
include: {style: true, manufacturer: true, containers: {include: {section: true}}},
|
|
where: where
|
|
}));
|
|
}
|
|
|
|
if(searchParams.has("drink", "Wine") || noDrinkSelected){
|
|
|
|
let where = Object.assign({}, whereBase, {style: {}});
|
|
|
|
// Beer styles
|
|
if(searchParams.has("winestyle")){
|
|
where.style = {id: {in: searchParams.getAll("winestyle").map(Number)}};
|
|
}
|
|
|
|
result = result.concat(await dbe.wine.findMany({
|
|
include: {style: true, manufacturer: true, containers: {include: {section: true}}},
|
|
where: where
|
|
}));
|
|
}
|
|
|
|
if(searchParams.has("drink", "Soda") || noDrinkSelected){
|
|
|
|
let where = whereBase;
|
|
|
|
// Carbonated
|
|
if(searchParams.has("carbonated")){
|
|
where = Object.assign({}, whereBase, {carbonated: true});
|
|
}
|
|
|
|
result = result.concat(await dbe.soda.findMany({
|
|
include: {manufacturer: true, containers: {include: {section: true}}},
|
|
where: where
|
|
}));
|
|
}
|
|
|
|
if(searchParams.has("drink", "Cocktail") || noDrinkSelected){
|
|
let where = whereBase;
|
|
|
|
// Carbonated
|
|
if(searchParams.has("mix")){
|
|
where = Object.assign({}, whereBase, {mix: true});
|
|
}
|
|
|
|
result = result.concat(await dbe.cocktail.findMany({
|
|
include: {manufacturer: true, containers: {include: {section: true}}},
|
|
where: where
|
|
}));
|
|
}
|
|
|
|
return result;
|
|
} |