-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatchSachetIngredientSachet.sql
98 lines (94 loc) · 2.99 KB
/
matchSachetIngredientSachet.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
runs on ingredientSachet create/update
runs on recipeHub call with sachets
input Array<{id: sachetId, quantity: "", ingredientName: "", processingName: ""}>
input Array<int> optional supplierItem ids
output {
id: 1,
data: {
matchedSachetIngredientSachet: Array<{sachet<payload>, ingredientSachet.id, isProcessingExactMatch}>
}
}
same as matchSachetSupplierItem but matches the sachet quantity not the supplierItem.unitSize
*/
CREATE OR REPLACE FUNCTION inventory."matchSachetIngredientSachet"(sachets jsonb, ingredientSachetIds integer []) RETURNS SETOF crm."customerData" LANGUAGE plpgsql STABLE AS $function$
DECLARE sachet_ingredient record;
sachet record;
result jsonb;
arr jsonb := '[]';
matched_sachet jsonb;
BEGIN IF ingredientSachetIds IS NOT NULL THEN FOR sachet_ingredient IN
-- select all sachets from ingreident.ingredientSachet
-- join ingredientProcessing and ingredient
-- filter out sachets with no quantity
-- filter in ingredients in ingredientIds
SELECT
"ingredientSachet".id,
quantity,
"processingName",
name
FROM
ingredient."ingredientSachet"
JOIN ingredient."ingredientProcessing" ON "ingredientProcessingId" = "ingredientProcessing".id
JOIN ingredient.ingredient ON "ingredientSachet"."ingredientId" = ingredient.id
WHERE
"ingredientSachet"."quantity" IS NOT NULL
AND "ingredientProcessing"."processingName" IS NOT NULL
AND "ingredientSachet".id = ANY (ingredientSachetIds)
SELECT
*
FROM
jsonb_array_elements(sachets) AS found_sachet
WHERE
(found_sachet ->> 'quantity') :: int = sachet_ingredient."quantity"
AND (found_sachet ->> 'processingName') = sachet_ingredient."processingName"
AND (found_sachet ->> 'ingredientName') = sachet_ingredient.name INTO matched_sachet;
IF matched_sachet IS NOT NULL THEN arr := arr || jsonb_build_object(
'sachet',
matched_sachet,
'ingredientSachetId',
sachet_ingredient.id,
'isProcessingExactMatch',
true
);
END IF;
END LOOP;
ELSE FOR sachet_ingredient IN
SELECT
"ingredientSachet".id,
quantity,
"processingName",
name
FROM
ingredient."ingredientSachet"
JOIN ingredient."ingredientProcessing" ON "ingredientProcessingId" = "ingredientProcessing".id
JOIN ingredient.ingredient ON "ingredientSachet"."ingredientId" = ingredient.id
WHERE
"ingredientSachet"."quantity" IS NOT NULL
AND "ingredientProcessing"."processingName" IS NOT NULL LOOP
SELECT
*
FROM
jsonb_array_elements(sachets) AS found_sachet
WHERE
(found_sachet ->> 'quantity') :: int = sachet_ingredient."quantity"
AND (found_sachet ->> 'processingName') = sachet_ingredient."processingName"
AND (found_sachet ->> 'ingredientName') = sachet_ingredient.name INTO matched_sachet;
IF matched_sachet IS NOT NULL THEN arr := arr || jsonb_build_object(
'sachet',
matched_sachet,
'ingredientSachetId',
sachet_ingredient.id,
'isProcessingExactMatch',
true
);
END IF;
END LOOP;
END IF;
result := jsonb_build_object('sachetIngredientSachetMatches', arr);
RETURN QUERY
SELECT
1 AS id,
result as data;
END;
$function$