Skip to content

Commit 30adc45

Browse files
committed
A bunch of stuff changed
- Fixed bug in create product - Refactored product preprocessor - Now get order endpoint can verify if order changed after a certain time, so it does not clog network
1 parent d1b2727 commit 30adc45

6 files changed

Lines changed: 122 additions & 83 deletions

File tree

.tmp/data.db

188 KB
Binary file not shown.

src/api/order/controllers/order.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ export default factories.createCoreController('api::order.order', ({ strapi }) =
9393
throw new ApplicationError("Missing field in request");
9494
}
9595

96-
const {accessCode,sessionCode} = ctx.request.body.data;
96+
const {accessCode,sessionCode,editedAfter} = ctx.request.body.data;
9797

9898
if(!accessCode || !sessionCode){
9999
throw new UnauthorizedError("Missing table detail");
@@ -109,7 +109,21 @@ export default factories.createCoreController('api::order.order', ({ strapi }) =
109109

110110
const orders = await strapi.service("api::order.order").getAllOrderByTable(tableID);
111111

112-
return orders.map(o => {
112+
if(editedAfter){
113+
const editedOrder = orders.filter(o => (new Date(o.updatedAt).getTime() > new Date(editedAfter).getTime()));
114+
115+
console.log(JSON.stringify(editedOrder,null,4));
116+
117+
if(editedOrder.length == 0){
118+
return {
119+
meta:{
120+
edited:false
121+
}
122+
}
123+
}
124+
}
125+
126+
const reducedOrder = orders.map(o => {
113127

114128
const prod = o.partial_orders.map((p) => p.product);
115129

@@ -120,6 +134,13 @@ export default factories.createCoreController('api::order.order', ({ strapi }) =
120134
products: prod,
121135
}
122136
});
137+
138+
return {
139+
data:reducedOrder,
140+
meta:{
141+
edited:true
142+
}
143+
}
123144
}
124145

125146
}));
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import { ProductIngredientDetail } from "../services/product";
2+
import { preprocessRules } from "./productPreprocessor";
3+
4+
const clientProductRule:preprocessRules =
5+
{
6+
Pizza: processPizza,
7+
Antipasto: deny,
8+
Bevanda: deny,
9+
}
10+
11+
export default clientProductRule;
12+
13+
//Basic deny creation function
14+
async function deny (product:any):Promise<null>{
15+
return null
16+
};
17+
18+
//Verify that pizza has only one base in the BaseID proprety
19+
async function processPizza(product: any):Promise<ProductIngredientDetail|null> {
20+
21+
//Reject invalid input
22+
if( !product.baseID || !product.ingredientsID || product.ingredientsID.lenght == 0){
23+
return null;
24+
}
25+
26+
//Checking ingredients detail
27+
let [base,ingredients] = await Promise.all([
28+
//Check Base
29+
strapi.documents("api::ingredient.ingredient").findOne(
30+
{
31+
documentId: product.baseID,
32+
fields: ["Type","Price"],
33+
}
34+
),
35+
//Check Ingredients
36+
strapi.documents("api::ingredient.ingredient").findMany(
37+
{
38+
fields: ["Type","Price"],
39+
filters:{
40+
documentId:product.ingredientsID,
41+
},
42+
}
43+
)
44+
]);
45+
46+
//Validate Ingredient, Only base must be of type pizza base
47+
const validBase = base ? base.Type === "pizza-base" : false;
48+
const validIg = ingredients.length > 0 && ingredients.filter((i) => i.Type === "pizza-base").length === 0;
49+
50+
if(validBase && validIg){
51+
//Calculate Price
52+
let price:number = base.Price;
53+
price += ingredients.reduce((tot,prod) => tot + prod.Price,0);
54+
//Merging base within the ingredients
55+
return {
56+
name: "Custom",
57+
price: price,
58+
categoryID:product.categoryID,
59+
ingredientsID:[product.baseID,...product.ingredientsID]
60+
};
61+
}else{
62+
return null;
63+
}
64+
}

src/api/product/controllers/product.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44

55
import { factories } from '@strapi/strapi'
66
import { errors } from '@strapi/utils';
7-
import productPreprocessor from './productPreprocessor';
8-
import { ingredientsAllergen } from '../services/product';
7+
import { productPreprocessor } from './productPreprocessor';
8+
import clientProductRule from './preprocessRules';
99

1010
const { ApplicationError, UnauthorizedError } = errors;
1111

12+
const clientPreprocessor = new productPreprocessor(clientProductRule);
13+
1214
interface Table{
1315
accessCode: string,
1416
sessionCode: string
@@ -18,6 +20,7 @@ export default factories.createCoreController('api::product.product', ( ({strapi
1820

1921
async createCustomProduct(ctx){
2022

23+
2124
if(!ctx.request.body || !ctx.request.body.table || !ctx.request.body.product)
2225
throw new ApplicationError("Missing field in request");
2326

@@ -29,7 +32,7 @@ export default factories.createCoreController('api::product.product', ( ({strapi
2932
throw new UnauthorizedError("Invalid table credential");
3033

3134
//Preprocess product before creation
32-
const processedProd = await productPreprocessor(product);
35+
const processedProd = await clientPreprocessor.process(product);
3336
if(!processedProd)
3437
throw new ApplicationError("Wrong Product Format");
3538

src/api/product/controllers/productPreprocessor.ts

Lines changed: 28 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -4,87 +4,38 @@
44

55
import { 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
}

src/extensions/documentation/documentation/1.0.0/full_documentation.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"name": "Apache 2.0",
1515
"url": "https://www.apache.org/licenses/LICENSE-2.0.html"
1616
},
17-
"x-generation-date": "2024-12-28T13:11:57.340Z"
17+
"x-generation-date": "2025-01-08T20:48:16.031Z"
1818
},
1919
"x-strapi-config": {
2020
"plugins": [

0 commit comments

Comments
 (0)