@megaads/wm
Version:
To install the library, use npm:
436 lines (421 loc) • 14.6 kB
text/typescript
import { PrintLocation } from './types';
interface Config {
[printLocation: string]: {
[locale: string]: {
default: {
value: number;
type: string;
};
category?: {
[categoryId: number]: {
value: number;
type: string;
};
};
};
};
}
class PrintLocations {
readonly options: PrintLocation[];
readonly locale: string;
readonly config: Config;
constructor(options: PrintLocation[], locale: string) {
this.options = options;
this.config = {
'chest_back': {
'us': {
'default': {
'value': 0.3,
'type': 'percent'
},
'category': {
7: {
'value': 5,
'type': 'plus'
},
33: {
'value': 5,
'type': 'plus'
},
8: {
'value': 9,
'type': 'plus'
},
151: {
'value': 9,
'type': 'plus'
},
48: {
'value': 5,
'type': 'plus'
},
54: {
'value': 8,
'type': 'plus'
},
68: {
'value': 5,
'type': 'plus'
}
}
},
'uk': {
'default': {
'value': 0.3,
'type': 'percent'
},
'category': {
7: {
'value': 5,
'type': 'plus'
},
33: {
'value': 5,
'type': 'plus'
},
8: {
'value': 9,
'type': 'plus'
},
82: {
'value': 9,
'type': 'plus'
},
48: {
'value': 5,
'type': 'plus'
},
54: {
'value': 8,
'type': 'plus'
},
65: {
'value': 5,
'type': 'plus'
}
}
},
'ca': {
'default': {
'value': 0.3,
'type': 'percent'
},
'category': {
7: {
'value': 5,
'type': 'plus'
},
33: {
'value': 5,
'type': 'plus'
},
8: {
'value': 9,
'type': 'plus'
},
84: {
'value': 9,
'type': 'plus'
},
48: {
'value': 5,
'type': 'plus'
},
54: {
'value': 8,
'type': 'plus'
},
68: {
'value': 5,
'type': 'plus'
}
}
},
'au': {
'default': {
'value': 0.3,
'type': 'percent'
},
'category': {
7: {
'value': 5,
'type': 'plus'
},
33: {
'value': 5,
'type': 'plus'
},
8: {
'value': 9,
'type': 'plus'
},
90: {
'value': 9,
'type': 'plus'
},
48: {
'value': 5,
'type': 'plus'
},
54: {
'value': 8,
'type': 'plus'
},
68: {
'value': 5,
'type': 'plus'
}
}
},
'de': {
'default': {
'value': 0.3,
'type': 'percent'
},
'category': {
7: {
'value': 5,
'type': 'plus'
},
33: {
'value': 5,
'type': 'plus'
},
8: {
'value': 9,
'type': 'plus'
},
48: {
'value': 5,
'type': 'plus'
},
54: {
'value': 8,
'type': 'plus'
},
63: {
'value': 5,
'type': 'plus'
}
}
},
'es': {
'default': {
'value': 0.3,
'type': 'percent'
},
'category': {
7: {
'value': 5,
'type': 'plus'
},
33: {
'value': 5,
'type': 'plus'
},
8: {
'value': 9,
'type': 'plus'
},
48: {
'value': 5,
'type': 'plus'
},
54: {
'value': 8,
'type': 'plus'
},
63: {
'value': 5,
'type': 'plus'
},
143: {
'value': 9,
'type': 'plus'
}
}
},
'fr': {
'default': {
'value': 0.3,
'type': 'percent'
},
'category': {
7: {
'value': 5,
'type': 'plus'
},
8: {
'value': 9,
'type': 'plus'
},
48: {
'value': 5,
'type': 'plus'
},
54: {
'value': 8,
'type': 'plus'
},
63: {
'value': 5,
'type': 'plus'
},
75: {
'value': 9,
'type': 'plus'
}
}
},
'it': {
'default': {
'value': 0.3,
'type': 'percent'
},
'category': {
7: {
'value': 5,
'type': 'plus'
},
33: {
'value': 5,
'type': 'plus'
},
63: {
'value': 9,
'type': 'plus'
},
154: {
'value': 9,
'type': 'plus'
},
66: {
'value': 5,
'type': 'plus'
},
62: {
'value': 8,
'type': 'plus'
},
70: {
'value': 5,
'type': 'plus'
}
}
},
'jp': {
'default': {
'value': 0.3,
'type': 'percent'
},
'category': {
7: {
'value': 800,
'type': 'plus'
},
8: {
'value': 1400,
'type': 'plus'
},
792: {
'value': 1400,
'type': 'plus'
},
48: {
'value': 800,
'type': 'plus'
},
54: {
'value': 1250,
'type': 'plus'
},
59: {
'value': 800,
'type': 'plus'
}
}
},
'pt': {
'default': {
'value': 0.3,
'type': 'percent'
}
},
'kr': {
'default': {
'value': 0.3,
'type': 'percent'
}
},
'vn': {
'default': {
'value': 0.3,
'type': 'percent'
}
}
}
};
this.locale = locale;
}
getAdditionalPrice(printLocation: string, categoryId: number, originalPrice: number) {
const configByLocation = this.config[printLocation];
if (!configByLocation) {
return originalPrice;
}
const configByLocale = configByLocation[this.locale];
if (!configByLocale) {
throw new Error('Locale not found');
}
let configByCategoryId = configByLocale.category ? configByLocale.category[categoryId] : null;
if (!configByCategoryId) {
configByCategoryId = configByLocale.default
}
let value = configByCategoryId.value;
let type = configByCategoryId.type;
if (type === 'percent') {
return originalPrice + originalPrice * value;
} else if (type === 'plus') {
return originalPrice + value;
} else {
throw new Error('Invalid type');
}
}
getGalleries(gallery: string[], printLocation: string): string[] {
gallery = gallery.map(item => {
return item.replace('/image/chest-', '/image/')
.replace('/image/chest-back-', '/image/')
.replace('/image/back-', '/image/');
});
let imageUrl = gallery[0];
if (this.isAllowDecorDomain(imageUrl) && printLocation !== 'front') {
let result = [];
let locations = printLocation.split('_');
if (printLocation.includes('_')) {
locations.unshift(printLocation.replace('_', '-'));
}
for (let location of locations) {
result.push(this.decorLiveviewWithPrintLocation(imageUrl, location));
}
gallery = result;
}
return gallery;
}
private isAllowDecorDomain(imageUrl: string) {
let allowDecorDomains = [
'gdn.printerval.com/image',
'cdn.printerval.com/image',
'liveview.printerval.com',
'liveview.prtvstatic.com'
];
for (let include of allowDecorDomains) {
if (imageUrl.includes(include)) {
return true;
}
}
return false;
}
private decorLiveviewWithPrintLocation(imageUrl: string, location: string) {
return imageUrl.replace('/image/', `/image/${location}-`);
}
}
export default PrintLocations