From 0bcba0be2e7ee513603db409dafdd9393e47d8c8 Mon Sep 17 00:00:00 2001 From: Peppa Date: Fri, 20 Nov 2026 15:37:02 +0500 Subject: [PATCH] =?UTF-8?q?TPTIP-83:=20=D1=83=D1=87=D0=B8=D1=82=D1=8B?= =?UTF-8?q?=D0=B2=D0=B0=D1=82=D1=8C=20=D1=86=D0=B5=D0=BB=D1=8C=20=D0=BF?= =?UTF-8?q?=D1=80=D0=BE=D1=84=D0=B8=D0=BB=D1=8F=20=D0=B2=20=D0=BC=D0=B5?= =?UTF-8?q?=D0=BD=D1=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.html | 1 + .../tptip-83-show-food-and-workout-menu/tasks.md | 2 +- profile/service.mjs | 12 ++++++++---- 3 files changed, 10 insertions(+), 5 deletions(-) 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); }