sanitize focus values (#52)
This commit is contained in:
@@ -18,7 +18,7 @@ import {
|
||||
ProductProportionsInput,
|
||||
} from '../types/product-types';
|
||||
import { Maybe } from '../types/types';
|
||||
import { booleanStringField, nullOrNumber } from './utils';
|
||||
import { booleanStringField, nullOrNumber, roundOrDefaultTo } from './utils';
|
||||
import { UserInputError } from 'apollo-server';
|
||||
|
||||
const MINUTE = 60;
|
||||
@@ -99,10 +99,15 @@ export class ProductAPI extends BaseSQLDataSource {
|
||||
fields['marginWidthMin'] = nullOrNumber(fields['marginWidthMin'] ?? null);
|
||||
fields['marginHeightMax'] = nullOrNumber(fields['marginHeightMax'] ?? null);
|
||||
fields['marginHeightMin'] = nullOrNumber(fields['marginHeightMin'] ?? null);
|
||||
fields['focusXpoint2'] = nullOrNumber(fields['focusXpoint2'] ?? null);
|
||||
fields['focusYpoint2'] = nullOrNumber(fields['focusYpoint2'] ?? null);
|
||||
fields['focusXpoint'] = nullOrNumber(fields['focusXpoint'] ?? null);
|
||||
fields['focusYpoint'] = nullOrNumber(fields['focusYpoint'] ?? null);
|
||||
|
||||
// Focus points values are in %.
|
||||
// Should be rounded to two decimals. Is sometimes missing in DB.
|
||||
// If missing should be treated as 50%, 50%
|
||||
fields['focusXpoint2'] = roundOrDefaultTo(fields['focusXpoint2'], 2, 50);
|
||||
fields['focusYpoint2'] = roundOrDefaultTo(fields['focusYpoint2'], 2, 50);
|
||||
|
||||
fields['photowallResolution'] = nullOrNumber(
|
||||
fields['photowallResolution'] ?? null,
|
||||
);
|
||||
|
||||
@@ -15,6 +15,11 @@ function isBoolean(n: InputType | boolean) {
|
||||
return false;
|
||||
}
|
||||
|
||||
function roundToDecimals(number: number, decimalPlaces: number) {
|
||||
const factor = Math.pow(10, decimalPlaces);
|
||||
return Math.round((number + Number.EPSILON) * factor) / factor;
|
||||
}
|
||||
|
||||
export function convertToPossibleType(n: InputType) {
|
||||
if ('' === n) return '';
|
||||
if (isBoolean(n)) return Boolean(n);
|
||||
@@ -32,3 +37,15 @@ export function booleanStringField(v: string | null) {
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
export function roundOrDefaultTo(
|
||||
v: string | null,
|
||||
decimalPlaces: number,
|
||||
defaultValue: number,
|
||||
) {
|
||||
if (!v) {
|
||||
return defaultValue;
|
||||
}
|
||||
const number = parseFloat(v);
|
||||
return roundToDecimals(number, decimalPlaces);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user