-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatchSachetSupplierItem.sql
76 lines (73 loc) · 2.29 KB
/
matchSachetSupplierItem.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
/*
runs on supplierItem 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: {
sachetSupplierItemMatches: Array<{sachetId, supplierItemId, isProcessingExactMatch}>
}
}
*/
CREATE OR REPLACE FUNCTION inventory."matchSachetSupplierItem"(sachets jsonb, supplierItemInputs integer []) RETURNS SETOF crm."customerData" LANGUAGE plpgsql STABLE AS $function$
DECLARE supplier_item record;
sachet record;
result jsonb;
arr jsonb := '[]';
matched_sachet jsonb;
BEGIN IF supplierItemInputs IS NOT NULL THEN FOR supplier_item IN
SELECT "supplierItem".id,
"supplierItem"."name",
"supplierItem"."unitSize",
"supplierItem".unit,
"processingName"
FROM inventory."supplierItem"
LEFT JOIN inventory."bulkItem" ON "bulkItemAsShippedId" = "bulkItem"."id"
WHERE "supplierItem".id = ANY (supplierItemInputs) LOOP
SELECT *
FROM jsonb_array_elements(sachets) AS found_sachet
WHERE (found_sachet->>'quantity')::int = supplier_item."unitSize"
AND (found_sachet->>'processingName') = supplier_item."processingName"
AND (found_sachet->>'ingredientName') = supplier_item.name INTO matched_sachet;
IF matched_sachet IS NOT NULL THEN arr := arr || jsonb_build_object(
'sachet',
matched_sachet,
'supplierItemId',
supplier_item.id,
'isProcessingExactMatch',
true
);
END IF;
END LOOP;
ELSE FOR supplier_item IN
SELECT "supplierItem".id,
"supplierItem"."name",
"supplierItem"."unitSize",
"supplierItem".unit,
"processingName"
FROM inventory."supplierItem"
LEFT JOIN inventory."bulkItem" ON "bulkItemAsShippedId" = "bulkItem"."id"
WHERE "processingName" IS NOT NULL LOOP
SELECT *
FROM jsonb_array_elements(sachets) AS found_sachet
WHERE (found_sachet->>'quantity')::int = supplier_item."unitSize"
AND (found_sachet->>'processingName') = supplier_item."processingName"
AND (found_sachet->>'ingredientName') = supplier_item.name INTO matched_sachet;
IF matched_sachet IS NOT NULL THEN arr := arr || jsonb_build_object(
'sachet',
matched_sachet,
'supplierItemId',
supplier_item.id,
'isProcessingExactMatch',
true
);
END IF;
END LOOP;
END IF;
result := jsonb_build_object('sachetSupplierItemMatches', arr);
RETURN QUERY
SELECT 1 AS id,
result as data;
END;
$function$