diff --git a/src/datasources/interior-api.ts b/src/datasources/interior-api.ts index c4bd28a..ae48552 100644 --- a/src/datasources/interior-api.ts +++ b/src/datasources/interior-api.ts @@ -1,7 +1,7 @@ import { ApolloServerErrorCode } from '@apollo/server/errors'; import { Interior, - InteriorsFilter, + InteriorsInputValues, InteriorType, } from '../types/interior-types'; import { Orientation } from '../types/product-types'; @@ -102,7 +102,9 @@ export class InteriorAPI extends BaseSQLDataSource { // Functions used in resolvers or other datasources // ---------------------------- - async getInteriors(filter: Maybe): Promise> { + async getInteriors( + input: Maybe, + ): Promise> { ScopeAccess.validate(this.user).some([ Scopes.INTERIORS_READ, Scopes.INTERIORS_PUBLIC_READ, @@ -127,10 +129,12 @@ export class InteriorAPI extends BaseSQLDataSource { }), ); - // Based on filter get valid interiors and filter out any else from all interiors - if (filter) { + // If input is set add data depending on what input contains. + // Right now it only contains productId which will result in + // the uri value is set to the s3 file if available. + if (input) { const validUris = await this.imageServerApi.getInteriorsForProduct( - filter.productId, + input.productId, ); const addZero = (number: number): string => { @@ -140,7 +144,7 @@ export class InteriorAPI extends BaseSQLDataSource { return `${number}`; }; - return allInteriors.filter((i: Interior) => { + return allInteriors.map((i: Interior) => { const it = i.type.toString().replace('_', '-').toLowerCase(); const or = i.orientation.toString().toLocaleLowerCase(); const uriMatch = `${or}/${it}/room${addZero(i.roomNumber)}.`; @@ -148,12 +152,11 @@ export class InteriorAPI extends BaseSQLDataSource { if (foundUri) { i.uri = foundUri.uri.indexOf('/') !== 0 ? `/${foundUri.uri}` : foundUri.uri; - return true; } - return false; + return i; }); } - // No filter so return all + // No input values so return all without modification return allInteriors; } diff --git a/src/datasources/interiors-lambda-api.ts b/src/datasources/interiors-lambda-api.ts index ffa4da2..8faae42 100644 --- a/src/datasources/interiors-lambda-api.ts +++ b/src/datasources/interiors-lambda-api.ts @@ -12,6 +12,27 @@ export class InteriorsLambdaAPI extends RESTDataSource { this.user = options.user; } + async generateInteriors(product: Product, roomIds: String[]): Promise { + ScopeAccess.validate(this.user).all([Scopes.INTERIORS_WRITE]); + + const body = { + token: process.env.INTERIORS_TOKEN, + image: `products/${product.id}.jpg`, + primary_image_area: [ + product.fields.focusXpoint2, + product.fields.focusYpoint2, + ], + autodetect: false, + rooms: roomIds, + }; + + if (isRepeatingPattern(product)) { + body['print_file_height'] = product.fields.printFileHeight; + body['print_file_dpi'] = product.fields.printFileDpi; + } + return this.post('/batch', { body: body }); + } + /** * * @param product Product with printProducts and fields diff --git a/src/resolvers/interiors-resolver.ts b/src/resolvers/interiors-resolver.ts index 5ba0a54..fdfaa48 100644 --- a/src/resolvers/interiors-resolver.ts +++ b/src/resolvers/interiors-resolver.ts @@ -1,10 +1,10 @@ import { InteriorAPI } from '../datasources/interior-api'; -import { InteriorsFilterInput } from '../types/interior-types'; +import { InteriorsInput } from '../types/interior-types'; const Interior = {}; -async function getInteriors(_, args: InteriorsFilterInput, { dataSources }) { - return (dataSources.interiorApi).getInteriors(args.filter); +async function getInteriors(_, args: InteriorsInput, { dataSources }) { + return (dataSources.interiorApi).getInteriors(args.input); } export const interiorTypeDefs = { Interior }; diff --git a/src/resolvers/products-resolver.ts b/src/resolvers/products-resolver.ts index 79a4f64..260afef 100644 --- a/src/resolvers/products-resolver.ts +++ b/src/resolvers/products-resolver.ts @@ -299,6 +299,22 @@ export const productMutationTypeDefs = { return product; }, + /** + * @function generateInteriors + * @description Will generate one interior for a specific product + */ + async generateInteriors(_, { productId, roomIds }, { dataSources }) { + const product = await (dataSources.productApi).getProduct( + productId, + ); + await (( + dataSources.interiorsLambdaApi + )).generateInteriors(product, roomIds); + return { + value: 'OK', + }; + }, + /** * @function productGroupInteriors * @description Mutate the list of interiors on a product and its groups. diff --git a/src/schema.graphql b/src/schema.graphql index d060bd1..4a3dd43 100644 --- a/src/schema.graphql +++ b/src/schema.graphql @@ -37,7 +37,7 @@ type Query { market(name: String!): Market locales: [Locale] locale(value: String!): Locale - interiors(filter: InteriorsFilterInput): [Interior] + interiors(input: InteriorsInput): [Interior] signedFileUploadURL(config: SignedUploadURLInput): SignedUploadURL @cacheControl(maxAge: 0) translate(input: TranslateInput!): StringResult! @@ -111,7 +111,7 @@ type ProductsSearchResult { references: [Product] } -input InteriorsFilterInput { +input InteriorsInput { productId: Int! } @@ -210,7 +210,9 @@ type OrderRowData { } type CategoryLocaleData { - path: String, name: String, locale: String + path: String + name: String + locale: String } type Category { @@ -242,7 +244,7 @@ type Copyright { } type Facet { - attribute: String, + attribute: String values: [String] } @@ -372,6 +374,9 @@ type Interior { roomName: String position: Int roomType: String + """ + uri is set when there is a generated file on s3, null otherwise + """ uri: String roomNumber: Int type: InteriorType @@ -518,6 +523,7 @@ type Mutation { """ productGroup(productId: Int!, groupIds: [Int]): Product productGroupInteriors(productId: Int!, groupId: Int!, uris: [String]): Product + generateInteriors(productId: Int!, roomIds: [String]!): StringResult addOwnInteriorToPrintProduct( printId: Int! uploadedS3Key: String! diff --git a/src/types/interior-types.ts b/src/types/interior-types.ts index d91f9c1..46c11a8 100644 --- a/src/types/interior-types.ts +++ b/src/types/interior-types.ts @@ -18,10 +18,10 @@ export enum InteriorType { FRAMED_PRINT, } -export interface InteriorsFilter { +export interface InteriorsInputValues { productId: number; } -export interface InteriorsFilterInput { - filter?: InteriorsFilter; +export interface InteriorsInput { + input?: InteriorsInputValues; }