@tbs-fitness/contract 0.5.0 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +33 -9
- package/dist/cjs/auth/auth.dto.js +5 -0
- package/dist/cjs/enums/index.d.ts +80 -0
- package/dist/cjs/enums/index.js +97 -1
- package/dist/cjs/feature-flags/feature-flag.dto.d.ts +9 -0
- package/dist/cjs/feature-flags/feature-flag.dto.js +9 -1
- package/dist/cjs/food/client-food.dto.d.ts +49 -0
- package/dist/cjs/food/client-food.dto.js +26 -0
- package/dist/cjs/food/food-import.dto.d.ts +160 -0
- package/dist/cjs/food/food-import.dto.js +110 -0
- package/dist/cjs/food/food-suggestion.dto.d.ts +374 -0
- package/dist/cjs/food/food-suggestion.dto.js +170 -0
- package/dist/cjs/food/food.dto.d.ts +339 -0
- package/dist/cjs/food/food.dto.js +256 -0
- package/dist/cjs/food/index.d.ts +5 -0
- package/dist/cjs/food/index.js +21 -0
- package/dist/cjs/food/nutrition.d.ts +75 -0
- package/dist/cjs/food/nutrition.js +158 -0
- package/dist/esm/auth/auth.dto.js +5 -0
- package/dist/esm/enums/index.d.ts +80 -0
- package/dist/esm/enums/index.js +96 -0
- package/dist/esm/feature-flags/feature-flag.dto.d.ts +9 -0
- package/dist/esm/feature-flags/feature-flag.dto.js +8 -0
- package/dist/esm/food/client-food.dto.d.ts +49 -0
- package/dist/esm/food/client-food.dto.js +23 -0
- package/dist/esm/food/food-import.dto.d.ts +160 -0
- package/dist/esm/food/food-import.dto.js +107 -0
- package/dist/esm/food/food-suggestion.dto.d.ts +374 -0
- package/dist/esm/food/food-suggestion.dto.js +167 -0
- package/dist/esm/food/food.dto.d.ts +339 -0
- package/dist/esm/food/food.dto.js +253 -0
- package/dist/esm/food/index.d.ts +5 -0
- package/dist/esm/food/index.js +5 -0
- package/dist/esm/food/nutrition.d.ts +75 -0
- package/dist/esm/food/nutrition.js +144 -0
- package/package.json +11 -1
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { type ServingUnit } from '../enums/index.js';
|
|
3
|
+
/**
|
|
4
|
+
* Nutrition rules shared by the backend and the apps, so a value the web form
|
|
5
|
+
* accepts is a value the API accepts, and both calculate derived values the
|
|
6
|
+
* same way.
|
|
7
|
+
*
|
|
8
|
+
* Library nutrients are stored per 100 of the serving unit (per 100 g, or per
|
|
9
|
+
* 100 ml for a serving measured in ml). Per-serving values are always derived.
|
|
10
|
+
*/
|
|
11
|
+
/** The largest nutrient amount per 100 units. Pure fat is 100 g; pure fat calories are 900. */
|
|
12
|
+
export declare const MAX_NUTRIENT_PER_100 = 9999.99;
|
|
13
|
+
export declare const MAX_SERVING_QUANTITY = 10000;
|
|
14
|
+
export declare const NUTRIENT_DECIMALS = 2;
|
|
15
|
+
/** Rounds to two decimals, the precision every nutrient is stored at. */
|
|
16
|
+
export declare function roundNutrient(value: number): number;
|
|
17
|
+
export declare function hasAtMostTwoDecimals(value: number): boolean;
|
|
18
|
+
/** A non-negative amount with at most two decimals. */
|
|
19
|
+
export declare const nutrientAmountSchema: (max?: number) => z.ZodNumber;
|
|
20
|
+
/** A positive quantity with at most two decimals, such as 200 (g) or 1.5 (bowls). */
|
|
21
|
+
export declare const quantitySchema: (max?: number) => z.ZodNumber;
|
|
22
|
+
/** The nutrients a library food records. Calories and the three macros are required. */
|
|
23
|
+
export interface NutrientValues {
|
|
24
|
+
calories: number;
|
|
25
|
+
protein: number;
|
|
26
|
+
carbs: number;
|
|
27
|
+
fat: number;
|
|
28
|
+
sugar?: number | null;
|
|
29
|
+
fiber?: number | null;
|
|
30
|
+
solubleFiber?: number | null;
|
|
31
|
+
insolubleFiber?: number | null;
|
|
32
|
+
}
|
|
33
|
+
/** The same values with every optional nutrient present, `null` when unknown. */
|
|
34
|
+
export interface CompleteNutrientValues {
|
|
35
|
+
calories: number;
|
|
36
|
+
protein: number;
|
|
37
|
+
carbs: number;
|
|
38
|
+
fat: number;
|
|
39
|
+
sugar: number | null;
|
|
40
|
+
fiber: number | null;
|
|
41
|
+
solubleFiber: number | null;
|
|
42
|
+
insolubleFiber: number | null;
|
|
43
|
+
}
|
|
44
|
+
/** Converts values for one serving of `servingQuantity` units into values per 100 units. */
|
|
45
|
+
export declare function toPer100(perServing: NutrientValues, servingQuantity: number): CompleteNutrientValues;
|
|
46
|
+
/** Converts values per 100 units into values for one serving of `servingQuantity` units. */
|
|
47
|
+
export declare function perServing(per100: NutrientValues, servingQuantity: number): CompleteNutrientValues;
|
|
48
|
+
/** Calories from the macros, using Biki's formula: (protein + carbs) × 4 + fat × 9. */
|
|
49
|
+
export declare function formulaCalories(protein: number, carbs: number, fat: number): number;
|
|
50
|
+
export interface NutritionIssue {
|
|
51
|
+
path: string;
|
|
52
|
+
message: string;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* The checks that hold for any food's values per 100 units: the macros cannot
|
|
56
|
+
* weigh more than the food, and sugar, fibre and its parts cannot exceed what
|
|
57
|
+
* contains them. The database enforces the same rules.
|
|
58
|
+
*/
|
|
59
|
+
export declare function nutritionIssues(per100: NutrientValues): NutritionIssue[];
|
|
60
|
+
/**
|
|
61
|
+
* The form of a food name used to find duplicates: case, accents of width,
|
|
62
|
+
* punctuation and spacing are ignored, so "Paneer (Full-Fat)" and
|
|
63
|
+
* "paneer full fat" match. Letters in any script are kept.
|
|
64
|
+
*/
|
|
65
|
+
export declare function normalizeFoodName(name: string): string;
|
|
66
|
+
/** The stable key for a metadata value label: "High Protein" becomes "high-protein". */
|
|
67
|
+
export declare function slugifyMetadataKey(label: string): string;
|
|
68
|
+
/**
|
|
69
|
+
* Reads the weight or volume from a serving label such as "1 cup (200g)" or
|
|
70
|
+
* "1 glass (250ml)". Returns null when the label states neither.
|
|
71
|
+
*/
|
|
72
|
+
export declare function parseServingLabel(label: string): {
|
|
73
|
+
quantity: number;
|
|
74
|
+
unit: ServingUnit;
|
|
75
|
+
} | null;
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.quantitySchema = exports.nutrientAmountSchema = exports.NUTRIENT_DECIMALS = exports.MAX_SERVING_QUANTITY = exports.MAX_NUTRIENT_PER_100 = void 0;
|
|
4
|
+
exports.roundNutrient = roundNutrient;
|
|
5
|
+
exports.hasAtMostTwoDecimals = hasAtMostTwoDecimals;
|
|
6
|
+
exports.toPer100 = toPer100;
|
|
7
|
+
exports.perServing = perServing;
|
|
8
|
+
exports.formulaCalories = formulaCalories;
|
|
9
|
+
exports.nutritionIssues = nutritionIssues;
|
|
10
|
+
exports.normalizeFoodName = normalizeFoodName;
|
|
11
|
+
exports.slugifyMetadataKey = slugifyMetadataKey;
|
|
12
|
+
exports.parseServingLabel = parseServingLabel;
|
|
13
|
+
const zod_1 = require("zod");
|
|
14
|
+
const index_js_1 = require("../enums/index.js");
|
|
15
|
+
/**
|
|
16
|
+
* Nutrition rules shared by the backend and the apps, so a value the web form
|
|
17
|
+
* accepts is a value the API accepts, and both calculate derived values the
|
|
18
|
+
* same way.
|
|
19
|
+
*
|
|
20
|
+
* Library nutrients are stored per 100 of the serving unit (per 100 g, or per
|
|
21
|
+
* 100 ml for a serving measured in ml). Per-serving values are always derived.
|
|
22
|
+
*/
|
|
23
|
+
/** The largest nutrient amount per 100 units. Pure fat is 100 g; pure fat calories are 900. */
|
|
24
|
+
exports.MAX_NUTRIENT_PER_100 = 9999.99;
|
|
25
|
+
exports.MAX_SERVING_QUANTITY = 10000;
|
|
26
|
+
exports.NUTRIENT_DECIMALS = 2;
|
|
27
|
+
const SCALE = 10 ** exports.NUTRIENT_DECIMALS;
|
|
28
|
+
const DECIMAL_TOLERANCE = 1e-6;
|
|
29
|
+
/** Rounds to two decimals, the precision every nutrient is stored at. */
|
|
30
|
+
function roundNutrient(value) {
|
|
31
|
+
return Math.round((value + Number.EPSILON) * SCALE) / SCALE;
|
|
32
|
+
}
|
|
33
|
+
function hasAtMostTwoDecimals(value) {
|
|
34
|
+
const scaled = value * SCALE;
|
|
35
|
+
return Math.abs(scaled - Math.round(scaled)) < DECIMAL_TOLERANCE;
|
|
36
|
+
}
|
|
37
|
+
const TWO_DECIMALS_ERROR = { error: 'Use at most 2 decimal places' };
|
|
38
|
+
/** A non-negative amount with at most two decimals. */
|
|
39
|
+
const nutrientAmountSchema = (max = exports.MAX_NUTRIENT_PER_100) => zod_1.z.number().min(0).max(max).refine(hasAtMostTwoDecimals, TWO_DECIMALS_ERROR);
|
|
40
|
+
exports.nutrientAmountSchema = nutrientAmountSchema;
|
|
41
|
+
/** A positive quantity with at most two decimals, such as 200 (g) or 1.5 (bowls). */
|
|
42
|
+
const quantitySchema = (max = exports.MAX_SERVING_QUANTITY) => zod_1.z.number().gt(0).max(max).refine(hasAtMostTwoDecimals, TWO_DECIMALS_ERROR);
|
|
43
|
+
exports.quantitySchema = quantitySchema;
|
|
44
|
+
function scale(values, factor) {
|
|
45
|
+
const optional = (value) => value === null || value === undefined
|
|
46
|
+
? null
|
|
47
|
+
: roundNutrient(value * factor);
|
|
48
|
+
return {
|
|
49
|
+
calories: roundNutrient(values.calories * factor),
|
|
50
|
+
protein: roundNutrient(values.protein * factor),
|
|
51
|
+
carbs: roundNutrient(values.carbs * factor),
|
|
52
|
+
fat: roundNutrient(values.fat * factor),
|
|
53
|
+
sugar: optional(values.sugar),
|
|
54
|
+
fiber: optional(values.fiber),
|
|
55
|
+
solubleFiber: optional(values.solubleFiber),
|
|
56
|
+
insolubleFiber: optional(values.insolubleFiber),
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
/** Converts values for one serving of `servingQuantity` units into values per 100 units. */
|
|
60
|
+
function toPer100(perServing, servingQuantity) {
|
|
61
|
+
return scale(perServing, 100 / servingQuantity);
|
|
62
|
+
}
|
|
63
|
+
/** Converts values per 100 units into values for one serving of `servingQuantity` units. */
|
|
64
|
+
function perServing(per100, servingQuantity) {
|
|
65
|
+
return scale(per100, servingQuantity / 100);
|
|
66
|
+
}
|
|
67
|
+
/** Calories from the macros, using Biki's formula: (protein + carbs) × 4 + fat × 9. */
|
|
68
|
+
function formulaCalories(protein, carbs, fat) {
|
|
69
|
+
return roundNutrient((protein + carbs) * 4 + fat * 9);
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* The checks that hold for any food's values per 100 units: the macros cannot
|
|
73
|
+
* weigh more than the food, and sugar, fibre and its parts cannot exceed what
|
|
74
|
+
* contains them. The database enforces the same rules.
|
|
75
|
+
*/
|
|
76
|
+
function nutritionIssues(per100) {
|
|
77
|
+
const issues = [];
|
|
78
|
+
if (per100.protein + per100.carbs + per100.fat > 100 + DECIMAL_TOLERANCE) {
|
|
79
|
+
issues.push({
|
|
80
|
+
path: 'fat',
|
|
81
|
+
message: 'Protein, carbs and fat together cannot exceed 100 per 100',
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
if ((per100.sugar ?? 0) > per100.carbs + DECIMAL_TOLERANCE) {
|
|
85
|
+
issues.push({ path: 'sugar', message: 'Sugar cannot exceed carbs' });
|
|
86
|
+
}
|
|
87
|
+
if ((per100.fiber ?? 0) > per100.carbs + DECIMAL_TOLERANCE) {
|
|
88
|
+
issues.push({ path: 'fiber', message: 'Fibre cannot exceed carbs' });
|
|
89
|
+
}
|
|
90
|
+
const fiberParts = (per100.solubleFiber ?? 0) + (per100.insolubleFiber ?? 0);
|
|
91
|
+
const hasParts = isPresent(per100.solubleFiber) || isPresent(per100.insolubleFiber);
|
|
92
|
+
if (hasParts && !isPresent(per100.fiber)) {
|
|
93
|
+
issues.push({
|
|
94
|
+
path: 'fiber',
|
|
95
|
+
message: 'Enter total fibre when entering soluble or insoluble fibre',
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
else if (fiberParts > (per100.fiber ?? 0) + DECIMAL_TOLERANCE) {
|
|
99
|
+
issues.push({
|
|
100
|
+
path: 'insolubleFiber',
|
|
101
|
+
message: 'Soluble and insoluble fibre together cannot exceed total fibre',
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
return issues;
|
|
105
|
+
}
|
|
106
|
+
function isPresent(value) {
|
|
107
|
+
return value !== null && value !== undefined;
|
|
108
|
+
}
|
|
109
|
+
const NON_NAME_CHARACTERS = /[^\p{L}\p{M}\p{N}]+/gu;
|
|
110
|
+
/**
|
|
111
|
+
* The form of a food name used to find duplicates: case, accents of width,
|
|
112
|
+
* punctuation and spacing are ignored, so "Paneer (Full-Fat)" and
|
|
113
|
+
* "paneer full fat" match. Letters in any script are kept.
|
|
114
|
+
*/
|
|
115
|
+
function normalizeFoodName(name) {
|
|
116
|
+
return name
|
|
117
|
+
.normalize('NFKC')
|
|
118
|
+
.toLowerCase()
|
|
119
|
+
.replace(NON_NAME_CHARACTERS, ' ')
|
|
120
|
+
.trim();
|
|
121
|
+
}
|
|
122
|
+
const METADATA_KEY_CHARACTERS = /[^a-z0-9]+/g;
|
|
123
|
+
const EDGE_HYPHENS = /^-+|-+$/g;
|
|
124
|
+
/** The stable key for a metadata value label: "High Protein" becomes "high-protein". */
|
|
125
|
+
function slugifyMetadataKey(label) {
|
|
126
|
+
return label
|
|
127
|
+
.normalize('NFKD')
|
|
128
|
+
.toLowerCase()
|
|
129
|
+
.replace(METADATA_KEY_CHARACTERS, '-')
|
|
130
|
+
.replace(EDGE_HYPHENS, '');
|
|
131
|
+
}
|
|
132
|
+
// A serving label usually puts the weight in brackets after a household
|
|
133
|
+
// measure, "1 cup sliced (100g)", and may mention other amounts outside them,
|
|
134
|
+
// "1 scoop (33g) in 200ml water". An amount in brackets wins; otherwise the
|
|
135
|
+
// first amount in the label.
|
|
136
|
+
const SERVING_AMOUNT = /(\d+(?:[.,]\d+)?)\s*(g|ml)\b/i;
|
|
137
|
+
const BRACKETED = /\(([^)]*)\)/g;
|
|
138
|
+
/**
|
|
139
|
+
* Reads the weight or volume from a serving label such as "1 cup (200g)" or
|
|
140
|
+
* "1 glass (250ml)". Returns null when the label states neither.
|
|
141
|
+
*/
|
|
142
|
+
function parseServingLabel(label) {
|
|
143
|
+
const bracketed = [...label.matchAll(BRACKETED)]
|
|
144
|
+
.map((group) => SERVING_AMOUNT.exec(group[1] ?? ''))
|
|
145
|
+
.find((match) => match !== null);
|
|
146
|
+
const [, amount, unitText] = bracketed ?? SERVING_AMOUNT.exec(label) ?? [];
|
|
147
|
+
if (amount === undefined || unitText === undefined) {
|
|
148
|
+
return null;
|
|
149
|
+
}
|
|
150
|
+
const quantity = Number(amount.replace(',', '.'));
|
|
151
|
+
const unit = unitText.toLowerCase();
|
|
152
|
+
if (!Number.isFinite(quantity) ||
|
|
153
|
+
quantity <= 0 ||
|
|
154
|
+
!index_js_1.SERVING_UNITS.includes(unit)) {
|
|
155
|
+
return null;
|
|
156
|
+
}
|
|
157
|
+
return { quantity: roundNutrient(quantity), unit };
|
|
158
|
+
}
|
|
@@ -87,6 +87,11 @@ export const accountResponseSchema = z
|
|
|
87
87
|
role: z.enum(ROLES),
|
|
88
88
|
/** @deprecated Always `[role]`. Kept for clients written before `role`. */
|
|
89
89
|
roles: z.array(z.string()),
|
|
90
|
+
/**
|
|
91
|
+
* Plain strings rather than `PERMISSIONS` values, because clients validate
|
|
92
|
+
* this response. With an enum, a client older than a new permission would
|
|
93
|
+
* reject the sign-in of everyone granted it.
|
|
94
|
+
*/
|
|
90
95
|
permissions: z.array(z.string()),
|
|
91
96
|
lastLoginAt: utcTimestampSchema.nullable(),
|
|
92
97
|
})
|
|
@@ -3,6 +3,37 @@ export type AppEnvironment = (typeof APP_ENVIRONMENTS)[number];
|
|
|
3
3
|
/** Each user has exactly one role. A new role is added here and nowhere else. */
|
|
4
4
|
export declare const ROLES: readonly ["ADMIN", "COACH", "CLIENT"];
|
|
5
5
|
export type Role = (typeof ROLES)[number];
|
|
6
|
+
/**
|
|
7
|
+
* Every permission name the API grants. Renaming one is a breaking change:
|
|
8
|
+
* clients compare these strings with what the API returns, so one on the old
|
|
9
|
+
* name silently loses whatever it guards. Which role has which permission is a
|
|
10
|
+
* server decision, so that mapping stays in the backend.
|
|
11
|
+
*/
|
|
12
|
+
export declare const PERMISSIONS: {
|
|
13
|
+
readonly accountsManage: "accounts:manage";
|
|
14
|
+
readonly rolesManage: "roles:manage";
|
|
15
|
+
readonly auditRead: "audit:read";
|
|
16
|
+
readonly featureFlagsManage: "feature_flags:manage";
|
|
17
|
+
readonly appSettingsManage: "app_settings:manage";
|
|
18
|
+
readonly communicationTemplatesManage: "communication_templates:manage";
|
|
19
|
+
readonly clientsReadAny: "clients:read:any";
|
|
20
|
+
readonly clientsWriteAny: "clients:write:any";
|
|
21
|
+
readonly clientsReadAssigned: "clients:read:assigned";
|
|
22
|
+
readonly clientsWriteAssigned: "clients:write:assigned";
|
|
23
|
+
readonly clientsReadSelf: "clients:read:self";
|
|
24
|
+
readonly clientsWriteSelf: "clients:write:self";
|
|
25
|
+
readonly foodsRead: "foods:read";
|
|
26
|
+
readonly foodsCreate: "foods:create";
|
|
27
|
+
readonly foodsUpdate: "foods:update";
|
|
28
|
+
readonly foodsDelete: "foods:delete";
|
|
29
|
+
readonly foodsImport: "foods:import";
|
|
30
|
+
readonly foodMetadataManage: "food_metadata:manage";
|
|
31
|
+
readonly foodSuggestionsReview: "food_suggestions:review";
|
|
32
|
+
readonly foodsSearchSelf: "foods:search:self";
|
|
33
|
+
readonly foodSuggestionsSubmitSelf: "food_suggestions:submit:self";
|
|
34
|
+
readonly foodSuggestionsReadSelf: "food_suggestions:read:self";
|
|
35
|
+
};
|
|
36
|
+
export type Permission = (typeof PERMISSIONS)[keyof typeof PERMISSIONS];
|
|
6
37
|
/**
|
|
7
38
|
* `pending_verification` is a self-registered user who has not yet proved they
|
|
8
39
|
* own their email address. Such a user cannot sign in.
|
|
@@ -21,6 +52,17 @@ export declare const TIMEZONE_SOURCES: readonly ["AUTO", "MANUAL"];
|
|
|
21
52
|
export type TimezoneSource = (typeof TIMEZONE_SOURCES)[number];
|
|
22
53
|
export declare const TOKEN_TRANSPORTS: readonly ["body", "cookie"];
|
|
23
54
|
export type TokenTransport = (typeof TOKEN_TRANSPORTS)[number];
|
|
55
|
+
/**
|
|
56
|
+
* The CSRF cookie the API sets, which scripts may read. The `__Host-` name is
|
|
57
|
+
* used when cookies are Secure, because browsers only honour that prefix on
|
|
58
|
+
* Secure cookies.
|
|
59
|
+
*/
|
|
60
|
+
export declare const CSRF_COOKIE_NAMES: {
|
|
61
|
+
readonly secure: "__Host-tbs_csrf";
|
|
62
|
+
readonly insecure: "tbs_csrf";
|
|
63
|
+
};
|
|
64
|
+
/** The header that carries the CSRF token back to the API. */
|
|
65
|
+
export declare const CSRF_HEADER = "x-csrf-token";
|
|
24
66
|
export declare const AUDIT_OUTCOMES: readonly ["success", "failure"];
|
|
25
67
|
export type AuditOutcome = (typeof AUDIT_OUTCOMES)[number];
|
|
26
68
|
export declare const FEATURE_FLAG_RULE_TYPES: readonly ["user", "client", "trainer", "role"];
|
|
@@ -44,3 +86,41 @@ export type DeliveryStatus = (typeof DELIVERY_STATUSES)[number];
|
|
|
44
86
|
/** Only one version of a template per message type and channel is active. */
|
|
45
87
|
export declare const TEMPLATE_STATUSES: readonly ["draft", "active", "archived"];
|
|
46
88
|
export type TemplateStatus = (typeof TEMPLATE_STATUSES)[number];
|
|
89
|
+
/** Who put a food into the library. */
|
|
90
|
+
export declare const FOOD_SOURCES: readonly ["ADMIN", "COACH", "CLIENT_SUGGESTION", "IMPORT"];
|
|
91
|
+
export type FoodSource = (typeof FOOD_SOURCES)[number];
|
|
92
|
+
/**
|
|
93
|
+
* The unit a library serving is measured in. Nutrients are stored per 100 of
|
|
94
|
+
* this unit, so a food in `ml` holds values per 100 ml.
|
|
95
|
+
*/
|
|
96
|
+
export declare const SERVING_UNITS: readonly ["g", "ml"];
|
|
97
|
+
export type ServingUnit = (typeof SERVING_UNITS)[number];
|
|
98
|
+
/** The portion units a client can pick when logging a food. */
|
|
99
|
+
export declare const PORTION_UNITS: readonly ["BOWL", "GRAM", "PIECE", "CUP"];
|
|
100
|
+
export type PortionUnit = (typeof PORTION_UNITS)[number];
|
|
101
|
+
/** Whether submitted nutrient values are per 100 units or per serving. */
|
|
102
|
+
export declare const NUTRITION_BASES: readonly ["PER_100", "PER_SERVING"];
|
|
103
|
+
export type NutritionBasis = (typeof NUTRITION_BASES)[number];
|
|
104
|
+
export declare const FOOD_SUGGESTION_STATUSES: readonly ["pending", "approved", "rejected", "withdrawn"];
|
|
105
|
+
export type FoodSuggestionStatus = (typeof FOOD_SUGGESTION_STATUSES)[number];
|
|
106
|
+
/** How an approved suggestion was resolved. */
|
|
107
|
+
export declare const FOOD_SUGGESTION_RESOLUTIONS: readonly ["ADDED_TO_LIBRARY", "LINKED_TO_EXISTING", "APPROVED_WITHOUT_LIBRARY"];
|
|
108
|
+
export type FoodSuggestionResolution = (typeof FOOD_SUGGESTION_RESOLUTIONS)[number];
|
|
109
|
+
/** What a bulk import does with a row whose food is already in the library. */
|
|
110
|
+
export declare const FOOD_IMPORT_MODES: readonly ["SKIP_DUPLICATES", "UPDATE_EXISTING"];
|
|
111
|
+
export type FoodImportMode = (typeof FOOD_IMPORT_MODES)[number];
|
|
112
|
+
export declare const FOOD_IMPORT_STATUSES: readonly ["previewed", "completed", "expired"];
|
|
113
|
+
export type FoodImportStatus = (typeof FOOD_IMPORT_STATUSES)[number];
|
|
114
|
+
export declare const FOOD_IMPORT_ROW_STATUSES: readonly ["valid", "invalid", "duplicate_in_file", "duplicate_existing"];
|
|
115
|
+
export type FoodImportRowStatus = (typeof FOOD_IMPORT_ROW_STATUSES)[number];
|
|
116
|
+
export declare const FOOD_IMPORT_FILE_TYPES: readonly ["csv", "xlsx"];
|
|
117
|
+
export type FoodImportFileType = (typeof FOOD_IMPORT_FILE_TYPES)[number];
|
|
118
|
+
export declare const FOOD_IMPORT_ISSUE_SEVERITIES: readonly ["error", "warning"];
|
|
119
|
+
export type FoodImportIssueSeverity = (typeof FOOD_IMPORT_ISSUE_SEVERITIES)[number];
|
|
120
|
+
/** Whether a food takes one value of a metadata type or several. */
|
|
121
|
+
export declare const FOOD_METADATA_SELECTIONS: readonly ["single", "multiple"];
|
|
122
|
+
export type FoodMetadataSelection = (typeof FOOD_METADATA_SELECTIONS)[number];
|
|
123
|
+
export declare const FOOD_SORT_FIELDS: readonly ["name", "updatedAt", "calories", "protein"];
|
|
124
|
+
export type FoodSortField = (typeof FOOD_SORT_FIELDS)[number];
|
|
125
|
+
export declare const SORT_ORDERS: readonly ["asc", "desc"];
|
|
126
|
+
export type SortOrder = (typeof SORT_ORDERS)[number];
|
package/dist/esm/enums/index.js
CHANGED
|
@@ -7,6 +7,36 @@ export const APP_ENVIRONMENTS = [
|
|
|
7
7
|
];
|
|
8
8
|
/** Each user has exactly one role. A new role is added here and nowhere else. */
|
|
9
9
|
export const ROLES = ['ADMIN', 'COACH', 'CLIENT'];
|
|
10
|
+
/**
|
|
11
|
+
* Every permission name the API grants. Renaming one is a breaking change:
|
|
12
|
+
* clients compare these strings with what the API returns, so one on the old
|
|
13
|
+
* name silently loses whatever it guards. Which role has which permission is a
|
|
14
|
+
* server decision, so that mapping stays in the backend.
|
|
15
|
+
*/
|
|
16
|
+
export const PERMISSIONS = {
|
|
17
|
+
accountsManage: 'accounts:manage',
|
|
18
|
+
rolesManage: 'roles:manage',
|
|
19
|
+
auditRead: 'audit:read',
|
|
20
|
+
featureFlagsManage: 'feature_flags:manage',
|
|
21
|
+
appSettingsManage: 'app_settings:manage',
|
|
22
|
+
communicationTemplatesManage: 'communication_templates:manage',
|
|
23
|
+
clientsReadAny: 'clients:read:any',
|
|
24
|
+
clientsWriteAny: 'clients:write:any',
|
|
25
|
+
clientsReadAssigned: 'clients:read:assigned',
|
|
26
|
+
clientsWriteAssigned: 'clients:write:assigned',
|
|
27
|
+
clientsReadSelf: 'clients:read:self',
|
|
28
|
+
clientsWriteSelf: 'clients:write:self',
|
|
29
|
+
foodsRead: 'foods:read',
|
|
30
|
+
foodsCreate: 'foods:create',
|
|
31
|
+
foodsUpdate: 'foods:update',
|
|
32
|
+
foodsDelete: 'foods:delete',
|
|
33
|
+
foodsImport: 'foods:import',
|
|
34
|
+
foodMetadataManage: 'food_metadata:manage',
|
|
35
|
+
foodSuggestionsReview: 'food_suggestions:review',
|
|
36
|
+
foodsSearchSelf: 'foods:search:self',
|
|
37
|
+
foodSuggestionsSubmitSelf: 'food_suggestions:submit:self',
|
|
38
|
+
foodSuggestionsReadSelf: 'food_suggestions:read:self',
|
|
39
|
+
};
|
|
10
40
|
/**
|
|
11
41
|
* `pending_verification` is a self-registered user who has not yet proved they
|
|
12
42
|
* own their email address. Such a user cannot sign in.
|
|
@@ -25,6 +55,17 @@ export const ADMIN_SETTABLE_ACCOUNT_STATUSES = ['active', 'disabled'];
|
|
|
25
55
|
*/
|
|
26
56
|
export const TIMEZONE_SOURCES = ['AUTO', 'MANUAL'];
|
|
27
57
|
export const TOKEN_TRANSPORTS = ['body', 'cookie'];
|
|
58
|
+
/**
|
|
59
|
+
* The CSRF cookie the API sets, which scripts may read. The `__Host-` name is
|
|
60
|
+
* used when cookies are Secure, because browsers only honour that prefix on
|
|
61
|
+
* Secure cookies.
|
|
62
|
+
*/
|
|
63
|
+
export const CSRF_COOKIE_NAMES = {
|
|
64
|
+
secure: '__Host-tbs_csrf',
|
|
65
|
+
insecure: 'tbs_csrf',
|
|
66
|
+
};
|
|
67
|
+
/** The header that carries the CSRF token back to the API. */
|
|
68
|
+
export const CSRF_HEADER = 'x-csrf-token';
|
|
28
69
|
export const AUDIT_OUTCOMES = ['success', 'failure'];
|
|
29
70
|
export const FEATURE_FLAG_RULE_TYPES = [
|
|
30
71
|
'user',
|
|
@@ -57,3 +98,58 @@ export const DELIVERY_STATUSES = [
|
|
|
57
98
|
];
|
|
58
99
|
/** Only one version of a template per message type and channel is active. */
|
|
59
100
|
export const TEMPLATE_STATUSES = ['draft', 'active', 'archived'];
|
|
101
|
+
/** Who put a food into the library. */
|
|
102
|
+
export const FOOD_SOURCES = [
|
|
103
|
+
'ADMIN',
|
|
104
|
+
'COACH',
|
|
105
|
+
'CLIENT_SUGGESTION',
|
|
106
|
+
'IMPORT',
|
|
107
|
+
];
|
|
108
|
+
/**
|
|
109
|
+
* The unit a library serving is measured in. Nutrients are stored per 100 of
|
|
110
|
+
* this unit, so a food in `ml` holds values per 100 ml.
|
|
111
|
+
*/
|
|
112
|
+
export const SERVING_UNITS = ['g', 'ml'];
|
|
113
|
+
/** The portion units a client can pick when logging a food. */
|
|
114
|
+
export const PORTION_UNITS = ['BOWL', 'GRAM', 'PIECE', 'CUP'];
|
|
115
|
+
/** Whether submitted nutrient values are per 100 units or per serving. */
|
|
116
|
+
export const NUTRITION_BASES = ['PER_100', 'PER_SERVING'];
|
|
117
|
+
export const FOOD_SUGGESTION_STATUSES = [
|
|
118
|
+
'pending',
|
|
119
|
+
'approved',
|
|
120
|
+
'rejected',
|
|
121
|
+
'withdrawn',
|
|
122
|
+
];
|
|
123
|
+
/** How an approved suggestion was resolved. */
|
|
124
|
+
export const FOOD_SUGGESTION_RESOLUTIONS = [
|
|
125
|
+
'ADDED_TO_LIBRARY',
|
|
126
|
+
'LINKED_TO_EXISTING',
|
|
127
|
+
'APPROVED_WITHOUT_LIBRARY',
|
|
128
|
+
];
|
|
129
|
+
/** What a bulk import does with a row whose food is already in the library. */
|
|
130
|
+
export const FOOD_IMPORT_MODES = [
|
|
131
|
+
'SKIP_DUPLICATES',
|
|
132
|
+
'UPDATE_EXISTING',
|
|
133
|
+
];
|
|
134
|
+
export const FOOD_IMPORT_STATUSES = [
|
|
135
|
+
'previewed',
|
|
136
|
+
'completed',
|
|
137
|
+
'expired',
|
|
138
|
+
];
|
|
139
|
+
export const FOOD_IMPORT_ROW_STATUSES = [
|
|
140
|
+
'valid',
|
|
141
|
+
'invalid',
|
|
142
|
+
'duplicate_in_file',
|
|
143
|
+
'duplicate_existing',
|
|
144
|
+
];
|
|
145
|
+
export const FOOD_IMPORT_FILE_TYPES = ['csv', 'xlsx'];
|
|
146
|
+
export const FOOD_IMPORT_ISSUE_SEVERITIES = ['error', 'warning'];
|
|
147
|
+
/** Whether a food takes one value of a metadata type or several. */
|
|
148
|
+
export const FOOD_METADATA_SELECTIONS = ['single', 'multiple'];
|
|
149
|
+
export const FOOD_SORT_FIELDS = [
|
|
150
|
+
'name',
|
|
151
|
+
'updatedAt',
|
|
152
|
+
'calories',
|
|
153
|
+
'protein',
|
|
154
|
+
];
|
|
155
|
+
export const SORT_ORDERS = ['asc', 'desc'];
|
|
@@ -140,3 +140,12 @@ export declare const killSwitchResponseSchema: z.ZodObject<{
|
|
|
140
140
|
updatedAt: z.ZodNullable<z.ZodISODateTime>;
|
|
141
141
|
}, z.core.$strip>;
|
|
142
142
|
export type KillSwitchResponse = z.output<typeof killSwitchResponseSchema>;
|
|
143
|
+
/**
|
|
144
|
+
* What a signed-in user may know about flags: only the keys that are on for
|
|
145
|
+
* them. It never holds rules, targeting values, rollout percentages or who
|
|
146
|
+
* changed what. A missing key means off, so always compare a value with `true`.
|
|
147
|
+
*/
|
|
148
|
+
export declare const evaluatedFlagsResponseSchema: z.ZodObject<{
|
|
149
|
+
flags: z.ZodRecord<z.ZodString, z.ZodBoolean>;
|
|
150
|
+
}, z.core.$strip>;
|
|
151
|
+
export type EvaluatedFlagsResponse = z.output<typeof evaluatedFlagsResponseSchema>;
|
|
@@ -131,3 +131,11 @@ export const killSwitchResponseSchema = z.object({
|
|
|
131
131
|
updatedBy: z.uuid().nullable(),
|
|
132
132
|
updatedAt: utcTimestampSchema.nullable(),
|
|
133
133
|
});
|
|
134
|
+
/**
|
|
135
|
+
* What a signed-in user may know about flags: only the keys that are on for
|
|
136
|
+
* them. It never holds rules, targeting values, rollout percentages or who
|
|
137
|
+
* changed what. A missing key means off, so always compare a value with `true`.
|
|
138
|
+
*/
|
|
139
|
+
export const evaluatedFlagsResponseSchema = z.object({
|
|
140
|
+
flags: z.record(z.string(), z.boolean()),
|
|
141
|
+
});
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
export declare const MIN_CLIENT_FOOD_SEARCH_LENGTH = 2;
|
|
3
|
+
export declare const clientFoodSearchQuerySchema: z.ZodObject<{
|
|
4
|
+
page: z.ZodDefault<z.ZodCoercedNumber<unknown>>;
|
|
5
|
+
pageSize: z.ZodDefault<z.ZodCoercedNumber<unknown>>;
|
|
6
|
+
search: z.ZodString;
|
|
7
|
+
}, z.core.$strip>;
|
|
8
|
+
export type ClientFoodSearchQuery = z.output<typeof clientFoodSearchQuerySchema>;
|
|
9
|
+
/** A library food as a client sees it: what they need to log it, and nothing about who added it. */
|
|
10
|
+
export declare const clientFoodResponseSchema: z.ZodObject<{
|
|
11
|
+
id: z.ZodUUID;
|
|
12
|
+
name: z.ZodString;
|
|
13
|
+
nameHi: z.ZodNullable<z.ZodString>;
|
|
14
|
+
namePa: z.ZodNullable<z.ZodString>;
|
|
15
|
+
englishAlias: z.ZodNullable<z.ZodString>;
|
|
16
|
+
serving: z.ZodObject<{
|
|
17
|
+
label: z.ZodString;
|
|
18
|
+
quantity: z.ZodNumber;
|
|
19
|
+
unit: z.ZodEnum<{
|
|
20
|
+
g: "g";
|
|
21
|
+
ml: "ml";
|
|
22
|
+
}>;
|
|
23
|
+
}, z.core.$strip>;
|
|
24
|
+
per100: z.ZodObject<{
|
|
25
|
+
calories: z.ZodNumber;
|
|
26
|
+
protein: z.ZodNumber;
|
|
27
|
+
carbs: z.ZodNumber;
|
|
28
|
+
fat: z.ZodNumber;
|
|
29
|
+
sugar: z.ZodNullable<z.ZodNumber>;
|
|
30
|
+
fiber: z.ZodNullable<z.ZodNumber>;
|
|
31
|
+
solubleFiber: z.ZodNullable<z.ZodNumber>;
|
|
32
|
+
insolubleFiber: z.ZodNullable<z.ZodNumber>;
|
|
33
|
+
}, z.core.$strip>;
|
|
34
|
+
perServing: z.ZodObject<{
|
|
35
|
+
calories: z.ZodNumber;
|
|
36
|
+
protein: z.ZodNumber;
|
|
37
|
+
carbs: z.ZodNumber;
|
|
38
|
+
fat: z.ZodNumber;
|
|
39
|
+
sugar: z.ZodNullable<z.ZodNumber>;
|
|
40
|
+
fiber: z.ZodNullable<z.ZodNumber>;
|
|
41
|
+
solubleFiber: z.ZodNullable<z.ZodNumber>;
|
|
42
|
+
insolubleFiber: z.ZodNullable<z.ZodNumber>;
|
|
43
|
+
}, z.core.$strip>;
|
|
44
|
+
tags: z.ZodArray<z.ZodObject<{
|
|
45
|
+
typeKey: z.ZodString;
|
|
46
|
+
label: z.ZodString;
|
|
47
|
+
}, z.core.$strip>>;
|
|
48
|
+
}, z.core.$strip>;
|
|
49
|
+
export type ClientFoodResponse = z.output<typeof clientFoodResponseSchema>;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { paginationQuerySchema } from '../common/pagination.schema.js';
|
|
3
|
+
import { MAX_FOOD_SEARCH_LENGTH, nutrientValuesResponseSchema, servingResponseSchema, } from './food.dto.js';
|
|
4
|
+
export const MIN_CLIENT_FOOD_SEARCH_LENGTH = 2;
|
|
5
|
+
export const clientFoodSearchQuerySchema = paginationQuerySchema.extend({
|
|
6
|
+
search: z
|
|
7
|
+
.string()
|
|
8
|
+
.trim()
|
|
9
|
+
.min(MIN_CLIENT_FOOD_SEARCH_LENGTH)
|
|
10
|
+
.max(MAX_FOOD_SEARCH_LENGTH),
|
|
11
|
+
});
|
|
12
|
+
/** A library food as a client sees it: what they need to log it, and nothing about who added it. */
|
|
13
|
+
export const clientFoodResponseSchema = z.object({
|
|
14
|
+
id: z.uuid(),
|
|
15
|
+
name: z.string(),
|
|
16
|
+
nameHi: z.string().nullable(),
|
|
17
|
+
namePa: z.string().nullable(),
|
|
18
|
+
englishAlias: z.string().nullable(),
|
|
19
|
+
serving: servingResponseSchema,
|
|
20
|
+
per100: nutrientValuesResponseSchema,
|
|
21
|
+
perServing: nutrientValuesResponseSchema,
|
|
22
|
+
tags: z.array(z.object({ typeKey: z.string(), label: z.string() })),
|
|
23
|
+
});
|