AddProduct mutation (#59)
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { BaseSQLDataSource } from './BaseSQLDataSource';
|
||||
import { camelcase } from 'stringcase';
|
||||
import slug from 'slug';
|
||||
import * as sql from './sql';
|
||||
import {
|
||||
Product,
|
||||
@@ -220,6 +221,23 @@ export class ProductAPI extends BaseSQLDataSource {
|
||||
);
|
||||
}
|
||||
|
||||
async addProduct(name: string, batch: string): Promise<number> {
|
||||
const path = batch ? batch + ' ' + name : name;
|
||||
const idres = await this.knex
|
||||
.table('product-products')
|
||||
.insert({
|
||||
path: slug(path),
|
||||
browsable: 0,
|
||||
visible: 0,
|
||||
})
|
||||
.returning('productid');
|
||||
const id = idres[0];
|
||||
this.updateProductField(id, 'artNo', `e${id}`);
|
||||
await this.updateProductField(id, 'name', name);
|
||||
await this.updateProductField(id, 'batch', batch);
|
||||
return id;
|
||||
}
|
||||
|
||||
async updateProductInfo(
|
||||
productId: number,
|
||||
info: ProductInfoInput,
|
||||
|
||||
@@ -84,6 +84,13 @@ export const productQueryTypeDefs = {
|
||||
///////////////////
|
||||
// Mutations below
|
||||
export const productMutationTypeDefs = {
|
||||
async addProduct(_, { name, batch }, { dataSources }) {
|
||||
const id = await (<ProductAPI>dataSources.productApi).addProduct(
|
||||
name,
|
||||
batch,
|
||||
);
|
||||
return (<ProductAPI>dataSources.productApi).getProduct(id);
|
||||
},
|
||||
async productInfo(_, { productId, info }, { dataSources }) {
|
||||
await (<ProductAPI>dataSources.productApi).updateProductInfo(
|
||||
productId,
|
||||
|
||||
@@ -431,6 +431,7 @@ type IdNumberResult {
|
||||
}
|
||||
|
||||
type Mutation {
|
||||
addProduct(name: String!, batch: String): Product
|
||||
productInfo(productId: Int!, info: ProductInfoInput!): Product
|
||||
productFocusPoint(
|
||||
productId: Int!
|
||||
|
||||
Reference in New Issue
Block a user