diff --git a/index.html b/index.html
index 31a7c4b..c6c1611 100644
--- a/index.html
+++ b/index.html
@@ -445,6 +445,7 @@ function applyPrivateProfile() {
const personal = window.TPTProfile;
delete db.settings.normsPending;
db.settings.profile = { ...personal.profile, sex: personal.profile.sex === 'male' ? 'm' : 'f' };
+ db.settings.goal = personal.profile.goal;
Object.assign(db.settings, suggestNorms(db.settings.goal, db.settings.profile), { onboarded: true });
db.settings.kcal = personal.nutrition.calories;
db.settings.protein = personal.nutrition.protein;
diff --git a/openspec/changes/tptip-83-show-food-and-workout-menu/tasks.md b/openspec/changes/tptip-83-show-food-and-workout-menu/tasks.md
index 96c011f..7d82e76 100644
--- a/openspec/changes/tptip-83-show-food-and-workout-menu/tasks.md
+++ b/openspec/changes/tptip-83-show-food-and-workout-menu/tasks.md
@@ -1,6 +1,6 @@
## 1. Реализация
-- [ ] 1.1 Сохранить цель в профиле и применить её к расчёту и дневным нормам.
+- [x] 1.1 Сохранить цель в профиле и применить её к расчёту и дневным нормам.
- [ ] 1.2 Добавить переход к меню из профиля и дневника, сохранить переход к плану и перевести подсказку регистрации.
## 2. Проверка
diff --git a/profile/service.mjs b/profile/service.mjs
index d86b46c..81e1682 100644
--- a/profile/service.mjs
+++ b/profile/service.mjs
@@ -5,7 +5,8 @@ const scrypt = promisify(derive);
export class InputError extends Error {
constructor(key, status = 400) { super(key); this.status = status; }
}
-export function validateProfile(input) {
+const goalFactors = {lose: 0.85, maintain: 1, gain: 1.1};
+export function validateProfile(input, defaultGoal = 'maintain') {
const out = {};
for (const [key, min, max] of [['height',100,250],['weight',25,350],['age',18,100]]) {
const value = input[key];
@@ -18,11 +19,14 @@ export function validateProfile(input) {
out.sex = input.sex;
if (!['1.2','1.375','1.55','1.725'].includes(String(input.activity))) throw new InputError('activity');
out.activity = Number(input.activity);
+ const goal = input.goal === undefined ? defaultGoal : input.goal;
+ if (typeof goal !== 'string' || !Object.hasOwn(goalFactors,goal)) throw new InputError('goal');
+ out.goal = goal;
return out;
}
export function nutrition(profile) {
const p = validateProfile(profile);
- const calories = Math.round((10*p.weight + 6.25*p.height - 5*p.age + (p.sex === 'male' ? 5 : -161))*p.activity);
+ const calories = Math.round((10*p.weight + 6.25*p.height - 5*p.age + (p.sex === 'male' ? 5 : -161))*p.activity*goalFactors[p.goal]);
const protein = Math.round(p.weight*1.6);
// One illustrative day: proportions sum to the daily energy estimate.
const menu = [
@@ -72,11 +76,11 @@ export class ProfileService {
return user;
}
read(token) {
- const user = this.user(token), profile = JSON.parse(user.profile);
+ const user = this.user(token), profile = validateProfile(JSON.parse(user.profile));
return {username:user.username,profile,nutrition:nutrition(profile)};
}
update(token,input) {
- const user = this.user(token), profile = validateProfile(input);
+ const user = this.user(token), profile = validateProfile(input,JSON.parse(user.profile).goal);
this.db.prepare('UPDATE users SET profile=? WHERE id=?').run(JSON.stringify(profile),user.id);
return this.read(token);
}