Added figure size conversions to actor revisions.

This commit is contained in:
2024-10-23 02:58:05 +02:00
parent 192aeeaafd
commit 39a7cf2ec0
3 changed files with 183 additions and 42 deletions

View File

@@ -4,6 +4,7 @@ import { unit } from 'mathjs';
import { MerkleJson } from 'merkle-json';
import moment from 'moment';
import omit from 'object.omit';
import convert from 'convert';
import initLogger from './logger.js';
import { knexOwner as knex, knexManticore } from './knex.js';
@@ -662,7 +663,77 @@ export async function reviewActorRevision(revisionId, isApproved, { feedback },
}
}
export async function createActorRevision(actorId, { edits, comment, apply }, reqUser) {
const cupConversions = {
us: ['AA', 'A', 'B', 'C', 'D', ['DD', 'E'], ['DDD', 'F'], 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P'], // United States
uk: ['AA', 'A', 'B', 'C', 'D', 'DD', 'E', 'F', 'FF', 'G', 'GG', 'H', 'HH', 'J', 'JJ', 'K', 'KK'], // United Kingdom
eu: ['AA', 'A', 'B', 'C', 'D', 'E', 'F', 'G', ' H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P'], // Europe
jp: ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q'], // Japan
};
cupConversions.fr = cupConversions.eu; // France
cupConversions.it = cupConversions.uk; // Italy
cupConversions.au = cupConversions.uk; // Australia
// bra band sizes
const bustConversions = {
us: [28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56],
eu: [60, 65, 70, 75, 80, 85, 90, 95, 100, 105, 110, 115, 120, 125, 130],
fr: [75, 80, 85, 90, 95, 100, 105, 110, 115, 120, 125, 130, 135, 140, 145],
it: [0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13],
au: [6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34],
};
bustConversions.uk = bustConversions.us;
bustConversions.jp = bustConversions.eu;
const conversions = {
cup: cupConversions,
bust: bustConversions,
};
// to US
function convertFigure(domain = 'cup', rawValue, units) {
if (!rawValue) {
return null;
}
const value = typeof rawValue === 'string'
? rawValue.toUpperCase()
: Number(rawValue);
if (!units || !cupConversions[units] || units === 'us') {
return value;
}
if (!conversions[domain]) {
if (['us', 'uk'].includes(units)) {
return value; // should already be in inches
}
return Math.round(convert(value, 'cm').to('inches'));
}
if (Number.isNaN(value)) {
return value;
}
const valueIndex = conversions[domain][units].findIndex((chartValue) => (Array.isArray(chartValue) // US uses both DD and E, and DDD and F
? chartValue.includes(value)
: value === chartValue));
const usValue = Array.isArray(conversions[domain].us[valueIndex])
? conversions[domain].us[valueIndex][0]
: conversions[domain].us[valueIndex];
return usValue;
}
export async function createActorRevision(actorId, {
edits,
comment,
apply,
...options
}, reqUser) {
const [
[actor],
openRevisions,
@@ -732,9 +803,29 @@ export async function createActorRevision(actorId, { edits, comment, apply }, re
return { key, value: Array.from(valueSet) };
}
return { key, value: value || null };
if (['cup', 'bust', 'waist', 'hip'].includes(key)) {
const convertedValue = convertFigure(key, value, options.figureUnits);
const conversionComment = convertedValue === value
? null
: `${key} converted from ${value} ${options.figureUnits?.toUpperCase() || 'US'} to ${convertedValue} US`;
return {
key,
value: convertedValue,
comment: conversionComment,
};
}
return {
key,
value: value || null,
};
}).filter(Boolean);
const deltaComments = deltas.map((delta) => delta.comment);
const curatedComment = [comment, ...deltaComments].filter(Boolean).join(' | ');
if (deltas.length === 0) {
throw new HttpError('No effective changes provided', 400);
}
@@ -749,7 +840,7 @@ export async function createActorRevision(actorId, { edits, comment, apply }, re
base: baseActor,
deltas,
}),
comment,
comment: curatedComment,
})
.returning('id');