Product info mutation (#14)
* Add mutation for product info * Upgrade to apollo 3
This commit is contained in:
@@ -10,11 +10,12 @@ import {
|
||||
ProductWallpaperType,
|
||||
ProductFields,
|
||||
Orientation,
|
||||
ProductsFilter,
|
||||
ProductsFilterInput,
|
||||
ProductInfoInput,
|
||||
} from '../types/product-types';
|
||||
import { Maybe } from '../types/types';
|
||||
import { convertToPossibleType } from './utils';
|
||||
import { isNode } from 'graphql/language/ast';
|
||||
|
||||
const MINUTE = 60;
|
||||
|
||||
@@ -88,7 +89,7 @@ export class ProductAPI extends BaseSQLDataSource {
|
||||
return Orientation[Orientation.LANDSCAPE];
|
||||
}
|
||||
if (fields.width < fields.height) {
|
||||
return Orientation[Orientation.PORTRAIT];
|
||||
return Orientation[Orientation.STANDING];
|
||||
}
|
||||
return Orientation[Orientation.SQUARE];
|
||||
}
|
||||
@@ -101,7 +102,6 @@ export class ProductAPI extends BaseSQLDataSource {
|
||||
row.fields = this.getProductFields(row);
|
||||
row.designerId = row.designerid;
|
||||
row.orientation = this.getOrientation(row.fields);
|
||||
row.fields_json = row.fields;
|
||||
return row;
|
||||
}
|
||||
|
||||
@@ -159,4 +159,45 @@ export class ProductAPI extends BaseSQLDataSource {
|
||||
|
||||
return ids.map(async (id) => this.getProduct(id));
|
||||
}
|
||||
|
||||
// mutations below
|
||||
private async updateProductField(
|
||||
productId: number,
|
||||
field: string,
|
||||
value: string,
|
||||
): Promise<void> {
|
||||
return this.knex.raw(
|
||||
/* sql */ `
|
||||
UPDATE "product-products_fields" SET value = ?
|
||||
WHERE productid = ?
|
||||
AND fieldid = (SELECT fieldid FROM "product-fields" WHERE field = ?)`,
|
||||
[value, productId, field],
|
||||
);
|
||||
}
|
||||
|
||||
async updateProductInfo(
|
||||
productId: number,
|
||||
info: ProductInfoInput,
|
||||
): Promise<any[]> {
|
||||
const promises = [];
|
||||
promises.push(
|
||||
this.knex
|
||||
.table('product-products')
|
||||
.update({
|
||||
path: info.path,
|
||||
browsable: info.browsable ? 1 : 0,
|
||||
visible: info.visible ? 1 : 0,
|
||||
designerid: info.designerId,
|
||||
ref1: info.ref1,
|
||||
ref2: info.ref2,
|
||||
ref3: info.ref3,
|
||||
})
|
||||
.where({ productid: productId }),
|
||||
this.updateProductField(productId, 'artNo', info.artNo),
|
||||
this.updateProductField(productId, 'name', info.name),
|
||||
this.updateProductField(productId, 'batch', info.batch),
|
||||
this.updateProductField(productId, 'copyright', info.copyright),
|
||||
);
|
||||
return Promise.all(promises);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user