More careful default types 2 (#37)
This commit is contained in:
@@ -47,7 +47,7 @@ export class InteriorAPI extends BaseSQLDataSource {
|
||||
}
|
||||
|
||||
getInteriorRoomNumber(roomNumberPart: string): number {
|
||||
return convertToPossibleType(roomNumberPart.slice(4));
|
||||
return parseInt(roomNumberPart.slice(4), 10);
|
||||
}
|
||||
|
||||
decorateRow(row: any): Interior {
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
import { RESTDataSource } from 'apollo-datasource-rest';
|
||||
import { Product, ProductGroup } from '../types/product-types';
|
||||
|
||||
export class InteriorsLambdaAPI extends RESTDataSource {
|
||||
constructor() {
|
||||
super();
|
||||
this.baseURL = process.env.INTERIOR_URL;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param product Product with printProducts and fields
|
||||
* Example usage in a resolver
|
||||
* <code>
|
||||
* const product = await (<ProductAPI>dataSources.productApi).getProduct(productId);
|
||||
* await (<InteriorsLambdaAPI>dataSources.interiorsLambdaAPI).generateNewInteriors(product);
|
||||
* </code>
|
||||
*/
|
||||
async generateNewInteriors(product: Product): Promise<any> {
|
||||
const found = product.printProducts.find(
|
||||
(pr) => pr.groupId === ProductGroup.WALLPAPER,
|
||||
);
|
||||
const isRepeating = found !== undefined;
|
||||
|
||||
const body = {
|
||||
token: process.env.INTERIOR_TOKEN,
|
||||
image: `products/${product.id}.jpg`,
|
||||
primary_image_area: [
|
||||
product.fields.focusXpoint2,
|
||||
product.fields.focusYpoint2,
|
||||
],
|
||||
autodetect: true,
|
||||
};
|
||||
|
||||
if (isRepeating) {
|
||||
body['print_file_height'] = product.fields.printFileHeight;
|
||||
body['print_file_dpi'] = product.fields.printFileDpi;
|
||||
}
|
||||
return this.post('/batch', body);
|
||||
}
|
||||
}
|
||||
@@ -66,7 +66,7 @@ export class OrderAPI extends BaseSQLDataSource {
|
||||
.count()
|
||||
.first()
|
||||
.cache(MINUTE * 60);
|
||||
return convertToPossibleType(res['count']); // Optimize later to return Promise
|
||||
return res['count'] as number; // Optimize later to return Promise
|
||||
}
|
||||
|
||||
async getOrders(input: Maybe<GeneralInput>): Promise<Array<Order>> {
|
||||
|
||||
@@ -17,7 +17,7 @@ import {
|
||||
ProductSizeTypes,
|
||||
} from '../types/product-types';
|
||||
import { Maybe } from '../types/types';
|
||||
import { convertToPossibleType } from './utils';
|
||||
import { convertToPossibleType, nullOrNumber } from './utils';
|
||||
|
||||
const MINUTE = 60;
|
||||
|
||||
@@ -75,11 +75,40 @@ export class ProductAPI extends BaseSQLDataSource {
|
||||
if (!row.fields) {
|
||||
return null; // 53460 for example
|
||||
}
|
||||
// Convert to camelCase first
|
||||
const fields = Object.keys(row.fields).reduce((mem, key) => {
|
||||
mem[camelcase(key)] = convertToPossibleType(row.fields[key]);
|
||||
mem[camelcase(key)] = row.fields[key];
|
||||
return mem;
|
||||
}, {});
|
||||
|
||||
fields['artNo'] = fields['artNo'];
|
||||
fields['name'] = fields['name'];
|
||||
fields['copyright'] = fields['copyright'] ?? '';
|
||||
fields['batch'] = fields['batch'] ?? '';
|
||||
fields['height'] = nullOrNumber(fields['height'] ?? null);
|
||||
fields['width'] = nullOrNumber(fields['width'] ?? null);
|
||||
fields['printFileWidth'] = nullOrNumber(fields['printFileWidth'] ?? null);
|
||||
fields['printFileHeight'] = nullOrNumber(fields['printFileHeight'] ?? null);
|
||||
fields['printFileDpi'] = nullOrNumber(fields['printFileDpi'] ?? null);
|
||||
fields['marginWidthMax'] = nullOrNumber(fields['marginWidthMax'] ?? null);
|
||||
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);
|
||||
fields['photowallResolution'] = nullOrNumber(
|
||||
fields['photowallResolution'] ?? null,
|
||||
);
|
||||
fields['canvasResolution'] = nullOrNumber(
|
||||
fields['canvasResolution'] ?? null,
|
||||
);
|
||||
fields['wallpaperResolution'] = nullOrNumber(
|
||||
fields['wallpaperResolution'] ?? null,
|
||||
);
|
||||
fields['proportionsWarning?'] = fields['proportionsWarning?'] ?? '';
|
||||
fields['imageResolution'] = nullOrNumber(fields['imageResolution'] ?? null);
|
||||
return <ProductFields>fields;
|
||||
}
|
||||
|
||||
@@ -104,8 +133,6 @@ export class ProductAPI extends BaseSQLDataSource {
|
||||
row.fields = this.getProductFields(row);
|
||||
row.designerId = row.designerid;
|
||||
row.orientation = this.getOrientation(row.fields);
|
||||
row.copyright = row.copyright ?? '';
|
||||
row.batch = row.batch ?? '';
|
||||
return row;
|
||||
}
|
||||
|
||||
@@ -114,7 +141,7 @@ export class ProductAPI extends BaseSQLDataSource {
|
||||
const res = await this.cachedRaw(query)
|
||||
.cache(MINUTE * 60)
|
||||
.then((data) => data.rows);
|
||||
return convertToPossibleType(res[0]['count']); // Optimize later to return Promise
|
||||
return res[0]['count'] as number; // Optimize later to return Promise
|
||||
}
|
||||
|
||||
async getProducts(input: ProductsFilterInput): Promise<Array<Product>> {
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
function isNumeric(n: any) {
|
||||
return !isNaN(n);
|
||||
type InputType = string | undefined | null;
|
||||
|
||||
function isNumeric(n: InputType) {
|
||||
return !isNaN(parseFloat(n));
|
||||
}
|
||||
|
||||
function isBoolean(n: any) {
|
||||
function isBoolean(n: InputType | boolean) {
|
||||
if (typeof n === 'boolean') {
|
||||
return true;
|
||||
}
|
||||
@@ -13,9 +15,13 @@ function isBoolean(n: any) {
|
||||
return false;
|
||||
}
|
||||
|
||||
export function convertToPossibleType(n: any) {
|
||||
export function convertToPossibleType(n: InputType) {
|
||||
if ('' === n) return '';
|
||||
if (isBoolean(n)) return Boolean(n);
|
||||
if (isNumeric(n)) return Number(n);
|
||||
return n;
|
||||
}
|
||||
|
||||
export function nullOrNumber(n: InputType) {
|
||||
return n === null || '' === n ? null : Number(n);
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@ import { InteriorAPI } from './datasources/interior-api';
|
||||
|
||||
import { verifyToken } from './cognito/cognito-client';
|
||||
import { readFileSync } from 'fs';
|
||||
import { InteriorsLambdaAPI } from './datasources/interiors-lambda-api';
|
||||
const path = require('path');
|
||||
const typeDefs = readFileSync(
|
||||
path.join(__dirname, './schema.graphql'),
|
||||
@@ -29,6 +30,7 @@ const knexConfig = knexStringcase(dbConfig);
|
||||
const categoryApi = new CategoryAPI(knexConfig);
|
||||
const designerApi = new DesignerAPI(knexConfig);
|
||||
const imageServerApi = new ImageServerApi();
|
||||
const interiorsLambdaApi = new InteriorsLambdaAPI();
|
||||
const interiorApi = new InteriorAPI(knexConfig, imageServerApi);
|
||||
const keywordApi = new KeywordAPI(knexConfig);
|
||||
const marketLocaleApi = new MarketLocaleAPI(knexConfig);
|
||||
@@ -39,6 +41,7 @@ const dataSources = () => ({
|
||||
categoryApi,
|
||||
designerApi,
|
||||
interiorApi,
|
||||
interiorsLambdaApi,
|
||||
imageServerApi,
|
||||
keywordApi,
|
||||
marketLocaleApi,
|
||||
|
||||
@@ -30,7 +30,6 @@ export const keywordsMutationTypeDefs = {
|
||||
name,
|
||||
type,
|
||||
);
|
||||
console.log(id);
|
||||
return (<KeywordAPI>dataSources.keywordApi).getKeywordById(id);
|
||||
},
|
||||
};
|
||||
|
||||
@@ -10,6 +10,7 @@ import { KeywordAPI } from '../datasources/keyword-api';
|
||||
import { Keyword } from '../types/keyword-types';
|
||||
import { MarketLocaleAPI } from '../datasources/market-locale-api';
|
||||
import { InteriorAPI } from '../datasources/interior-api';
|
||||
import { InteriorsLambdaAPI } from '../datasources/interiors-lambda-api';
|
||||
|
||||
const ProductBlacklist = {
|
||||
async market(parent, _args, { dataSources }) {
|
||||
@@ -111,7 +112,13 @@ export const productMutationTypeDefs = {
|
||||
productId,
|
||||
groupIds,
|
||||
);
|
||||
return (<ProductAPI>dataSources.productApi).getProduct(productId);
|
||||
const product = await (<ProductAPI>dataSources.productApi).getProduct(
|
||||
productId,
|
||||
);
|
||||
await (<InteriorsLambdaAPI>(
|
||||
dataSources.interiorsLambdaApi
|
||||
)).generateNewInteriors(product);
|
||||
return product;
|
||||
},
|
||||
async productKeywords(_, { productId, keywordIds }, { dataSources }) {
|
||||
await (<KeywordAPI>dataSources.keywordApi).setProductKeywords(
|
||||
|
||||
+1
-1
@@ -267,7 +267,7 @@ type ProductFields {
|
||||
focusYpoint: Float
|
||||
focusXpoint2: Float
|
||||
focusYpoint2: Float
|
||||
batch: String!
|
||||
batch: String
|
||||
imageResolution: Int
|
||||
printFileWidth: Int
|
||||
printFileHeight: Int
|
||||
|
||||
Reference in New Issue
Block a user