44
55import { ProductIngredientDetail } from "../services/product" ;
66
7- //Contains preprocess function for each category
8- const categoryPreprocessRule : { [ key :string ] : ( any ) => Promise < ProductIngredientDetail | null > } =
9- {
10- Pizza : processPizza ,
11- Antipasto : deny ,
12- Bevanda : deny ,
7+ export interface preprocessRules {
8+ [ key :string ] : ( any ) => Promise < ProductIngredientDetail | null >
139}
1410
15- //Process product to add based on their category
16- export default async function productPreprocessor ( product :any ) {
17- //Reject product without category, Product must have one
18- if ( ! product . categoryID )
19- return null ;
20-
21- //Fetch categoty name
22- const categoryName :string = await strapi . documents ( "api::category.category" )
23- . findOne ( {
24- documentId :product . categoryID ,
25- fields :[ "Name" ] ,
26- } )
27- . then ( ( category ) => category . Name ) ;
28-
29- if ( categoryName ) {
30- console . log ( "Creating: " , categoryName ) ;
31- //Apply preprocess function to product
32- return await categoryPreprocessRule [ categoryName ] ( product ) ;
33- } else {
34- console . log ( "No category found" ) ;
35- return null ;
36- }
37- }
38-
39- //Basic deny creation function
40- async function deny ( product :any ) :Promise < null > {
41- return null
42- } ;
43-
44- //Verify that pizza has only one base in the BaseID proprety
45- async function processPizza ( product : any ) :Promise < ProductIngredientDetail | null > {
11+ export class productPreprocessor {
4612
47- //Reject invalid input
48- if ( ! product . baseID || ! product . ingredientsID || product . ingredientsID . lenght == 0 ) {
49- return null ;
50- }
13+ private ruleset :preprocessRules ;
5114
52- //Checking ingredients detail
53- let [ base , ingredients ] = await Promise . all ( [
54- //Check Base
55- strapi . documents ( "api::ingredient.ingredient" ) . findOne (
56- {
57- documentId : product . baseID ,
58- fields : [ "Type" , "Price" ] ,
59- }
60- ) ,
61- //Check Ingredients
62- strapi . documents ( "api::ingredient.ingredient" ) . findMany (
63- {
64- fields : [ "Type" , "Price" ] ,
65- filters :{
66- documentId :product . ingredientsID ,
67- } ,
68- }
69- )
70- ] ) ;
71-
72- //Validate Ingredient, Only base must be of type pizza base
73- const validBase = base ? base . Type === "pizza-base" : false ;
74- const validIg = ingredients . length > 0 && ingredients . filter ( ( i ) => i . Type === "pizza-base" ) . length === 0 ;
15+ constructor ( ruleset :preprocessRules ) {
16+ this . ruleset = ruleset ;
17+ }
7518
76- if ( validBase && validIg ) {
77- //Calculate Price
78- let price :number = base . Price ;
79- price += ingredients . reduce ( ( tot , prod ) => tot + prod . Price , 0 ) ;
80- //Merging base within the ingredients
81- return {
82- name : "Custom" ,
83- price : price ,
84- categoryID :product . categoryID ,
85- ingredientsID :[ product . baseID , ...product . ingredientsID ]
86- } ;
87- } else {
88- return null ;
19+ //Process product to add based on their category
20+ async process ( product :any ) :Promise < ProductIngredientDetail | null > {
21+ //Reject product without category, Product must have one
22+ if ( ! product . categoryID )
23+ return null ;
24+
25+ //Fetch categoty name
26+ const category = await strapi . documents ( "api::category.category" )
27+ . findOne ( {
28+ documentId :product . categoryID ,
29+ fields :[ "Name" ] ,
30+ } )
31+
32+ if ( category && this . ruleset [ category . Name ] ) {
33+ console . log ( "Creating: " , category . Name ) ;
34+ //Apply preprocess function to product
35+ return await this . ruleset [ category . Name ] ( product ) ;
36+ } else {
37+ console . log ( "No category found" ) ;
38+ return null ;
39+ }
8940 }
9041}
0 commit comments