TPTIP-83: учитывать цель профиля в меню
This commit is contained in:
parent
c30a8d6501
commit
0bcba0be2e
@ -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;
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
## 1. Реализация
|
||||
|
||||
- [ ] 1.1 Сохранить цель в профиле и применить её к расчёту и дневным нормам.
|
||||
- [x] 1.1 Сохранить цель в профиле и применить её к расчёту и дневным нормам.
|
||||
- [ ] 1.2 Добавить переход к меню из профиля и дневника, сохранить переход к плану и перевести подсказку регистрации.
|
||||
|
||||
## 2. Проверка
|
||||
|
||||
@ -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);
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user