Skip to content

Commit 16c72e6

Browse files
authored
Merge pull request #1407 from CodeWithCJ/dev
Fix for missing user id column in food variant
2 parents 80ae536 + 9df98d6 commit 16c72e6

5 files changed

Lines changed: 11 additions & 26 deletions

File tree

SparkyFitnessFrontend/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "sparkyfitnessfrontend",
33
"private": true,
4-
"version": "0.16.7",
4+
"version": "0.16.8",
55
"homepage": "https://github.com/CodeWithCJ/SparkyFitness",
66
"type": "module",
77
"scripts": {

SparkyFitnessServer/models/food.ts

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const DEFAULT_VARIANT_JSON_SQL = `
2727
'is_default', fv.is_default,
2828
'glycemic_index', fv.glycemic_index,
2929
'custom_nutrients', fv.custom_nutrients,
30-
'user_id', fv.user_id,
30+
'user_id', f.user_id,
3131
'source', fv.source,
3232
'ai_confidence', fv.ai_confidence,
3333
'allergens', fv.allergens,
@@ -42,11 +42,6 @@ const PREFERRED_DEFAULT_VARIANT_JOIN_SQL = `
4242
WHERE candidate_fv.food_id = f.id
4343
AND candidate_fv.is_default = TRUE
4444
ORDER BY
45-
CASE
46-
WHEN candidate_fv.user_id = current_user_id() THEN 0
47-
WHEN candidate_fv.user_id = f.user_id THEN 1
48-
ELSE 2
49-
END,
5045
candidate_fv.updated_at DESC,
5146
candidate_fv.id
5247
LIMIT 1
@@ -186,15 +181,14 @@ async function createFood(foodData: any) {
186181
// 2. Create the primary food variant and mark it as default
187182
const variantResult = await client.query(
188183
`INSERT INTO food_variants (
189-
food_id, user_id, serving_size, serving_unit, calories, protein, carbs, fat,
184+
food_id, serving_size, serving_unit, calories, protein, carbs, fat,
190185
saturated_fat, polyunsaturated_fat, monounsaturated_fat, trans_fat,
191186
cholesterol, sodium, potassium, dietary_fiber, sugars,
192187
vitamin_a, vitamin_c, calcium, iron, is_default, glycemic_index, custom_nutrients,
193188
source, ai_confidence, allergens, traces, created_at, updated_at
194-
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21, TRUE, $22, $23, $24, $25, $26, $27, now(), now()) RETURNING id`,
189+
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, TRUE, $21, $22, $23, $24, $25, $26, now(), now()) RETURNING id`,
195190
[
196191
newFood.id,
197-
newFood.user_id,
198192
sanitizeNumeric(foodData.serving_size),
199193
foodData.serving_unit,
200194
sanitizeNumeric(foodData.calories),
@@ -809,19 +803,17 @@ async function createFoodsInBulk(userId: any, foodDataArray: any) {
809803
for (const variant of food.variants) {
810804
await client.query(
811805
`INSERT INTO food_variants (
812-
food_id, user_id, serving_size, serving_unit, is_default, calories, protein, carbs, fat,
806+
food_id, serving_size, serving_unit, is_default, calories, protein, carbs, fat,
813807
saturated_fat, polyunsaturated_fat, monounsaturated_fat, trans_fat,
814808
cholesterol, sodium, potassium, dietary_fiber, sugars,
815809
vitamin_a, vitamin_c, calcium, iron, glycemic_index, custom_nutrients,
816810
source, ai_confidence, allergens, traces, created_at, updated_at
817811
) VALUES (
818812
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14,
819-
$15, $16, $17, $18, $19, $20, $21, $22, $23, $24, $25, $26, $27, now(), now()
813+
$15, $16, $17, $18, $19, $20, $21, $22, $23, $24, $25, $26, now(), now()
820814
)`,
821815
[
822816
newFoodId,
823-
// @ts-expect-error TS(2571): Object is of type 'unknown'.
824-
food.user_id,
825817
sanitizeNumeric(variant.serving_size),
826818
variant.serving_unit,
827819
sanitizeBoolean(variant.is_default) ?? true,

SparkyFitnessServer/models/foodMisc.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const DEFAULT_VARIANT_JSON_SQL = `
2525
'is_default', fv.is_default,
2626
'glycemic_index', fv.glycemic_index,
2727
'custom_nutrients', fv.custom_nutrients,
28-
'user_id', fv.user_id,
28+
'user_id', f.user_id,
2929
'source', fv.source,
3030
'ai_confidence', fv.ai_confidence
3131
) AS default_variant
@@ -38,11 +38,6 @@ const PREFERRED_DEFAULT_VARIANT_JOIN_SQL = `
3838
WHERE candidate_fv.food_id = f.id
3939
AND candidate_fv.is_default = TRUE
4040
ORDER BY
41-
CASE
42-
WHEN candidate_fv.user_id = current_user_id() THEN 0
43-
WHEN candidate_fv.user_id = f.user_id THEN 1
44-
ELSE 2
45-
END,
4641
candidate_fv.updated_at DESC,
4742
candidate_fv.id
4843
LIMIT 1

SparkyFitnessServer/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "sparkyfitnessserver",
3-
"version": "0.16.7",
3+
"version": "0.16.8",
44
"main": "SparkyFitnessServer.js",
55
"type": "module",
66
"scripts": {

SparkyFitnessServer/tests/foodDefaultVariantQueries.test.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,13 @@ describe('preferred default variant queries', () => {
2626
vi.clearAllMocks();
2727
});
2828

29-
it('searchFoods prefers a single viewer-specific default variant via lateral join', async () => {
29+
it('searchFoods prefers a default variant via lateral join', async () => {
3030
await searchFoods('yogurt', userId, false, true, false, 10);
3131

3232
const queryStr = mockClient.query.mock.calls[0][0];
3333
expect(queryStr).toContain('LEFT JOIN LATERAL');
34-
expect(queryStr).toContain(
35-
'WHEN candidate_fv.user_id = current_user_id() THEN 0'
36-
);
37-
expect(queryStr).toContain('WHEN candidate_fv.user_id = f.user_id THEN 1');
34+
expect(queryStr).toContain('candidate_fv.food_id = f.id');
35+
expect(queryStr).toContain('candidate_fv.is_default = TRUE');
3836
expect(queryStr).not.toContain(
3937
'LEFT JOIN food_variants fv ON f.id = fv.food_id AND fv.is_default = TRUE'
4038
);

0 commit comments

Comments
 (0)