Skip to content

Commit

Permalink
Merge pull request #14 from labd/feat/restockable-in-days
Browse files Browse the repository at this point in the history
fix linting, add restockableInDays, fix order number endpoint not expanding
  • Loading branch information
davidweterings authored May 6, 2021
2 parents 3d76643 + 8e8af12 commit fcf5dbe
Show file tree
Hide file tree
Showing 8 changed files with 154 additions and 28 deletions.
4 changes: 2 additions & 2 deletions src/repositories/abstract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import { AbstractStorage } from '../storage'
import { checkConcurrentModification } from './errors'
import { CommercetoolsError } from '../exceptions'

type QueryParams = {
export type QueryParams = {
expand?: string[]
where?: string[]
}

type GetParams = {
export type GetParams = {
expand?: string[]
}

Expand Down
8 changes: 8 additions & 0 deletions src/repositories/inventory-entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
InventoryEntrySetCustomFieldAction,
InventoryEntrySetCustomTypeAction,
InventoryEntrySetExpectedDeliveryAction,
InventoryEntrySetRestockableInDaysAction,
ReferenceTypeId,
} from '@commercetools/platform-sdk'
import { getBaseResourceProperties } from '../helpers'
Expand Down Expand Up @@ -84,5 +85,12 @@ export class InventoryEntryRepository extends AbstractRepository {
}
}
},
setRestockableInDays: (
projectKey: string,
resource: Writable<InventoryEntry>,
{ restockableInDays }: InventoryEntrySetRestockableInDaysAction
) => {
resource.restockableInDays = restockableInDays
},
}
}
10 changes: 6 additions & 4 deletions src/repositories/order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
ReferenceTypeId,
Store,
} from '@commercetools/platform-sdk'
import AbstractRepository from './abstract'
import AbstractRepository, { QueryParams } from './abstract'
import {
createCustomFields,
createPrice,
Expand All @@ -42,7 +42,7 @@ export class OrderRepository extends AbstractRepository {
}

create(projectKey: string, draft: OrderFromCartDraft): Order {
assert(draft.cart)
assert(draft.cart, 'draft.cart is missing')

const cart = this._storage.getByResourceIdentifier(
projectKey,
Expand Down Expand Up @@ -70,7 +70,7 @@ export class OrderRepository extends AbstractRepository {

import(projectKey: string, draft: OrderImportDraft): Order {
// TODO: Check if order with given orderNumber already exists
assert(this)
assert(this, 'OrderRepository not valid')
const resource: Order = {
...getBaseResourceProperties(),

Expand Down Expand Up @@ -192,9 +192,11 @@ export class OrderRepository extends AbstractRepository {

getWithOrderNumber(
projectKey: string,
orderNumber: string
orderNumber: string,
params: QueryParams = {}
): Order | undefined {
const result = this._storage.query(projectKey, this.getTypeId(), {
...params,
where: [`orderNumber="${orderNumber}"`],
})
if (result.count === 1) {
Expand Down
29 changes: 21 additions & 8 deletions src/services/inventory-entry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe('Inventory Entry Query', () => {
})

test('no filter', async () => {
assert(inventoryEntry)
assert(inventoryEntry, 'inventory entry not created')

const response = await supertest(ctMock.app).get(`/dummy/inventory`)
expect(response.status).toBe(200)
Expand All @@ -34,7 +34,7 @@ describe('Inventory Entry Query', () => {
})

test('filter sku', async () => {
assert(inventoryEntry)
assert(inventoryEntry, 'inventory entry not created')

{
const response = await supertest(ctMock.app)
Expand Down Expand Up @@ -82,7 +82,7 @@ describe('Inventory Entry Update Actions', () => {
})

test('changeQuantity', async () => {
assert(inventoryEntry)
assert(inventoryEntry, 'inventory entry not created')

const response = await supertest(ctMock.app)
.post(`/dummy/inventory/${inventoryEntry.id}`)
Expand All @@ -97,8 +97,8 @@ describe('Inventory Entry Update Actions', () => {
})

test('set custom type', async () => {
assert(inventoryEntry)
assert(customType)
assert(inventoryEntry, 'inventory entry not created')
assert(customType, 'custom type not created')

const response = await supertest(ctMock.app)
.post(`/dummy/inventory/${inventoryEntry.id}`)
Expand All @@ -117,7 +117,7 @@ describe('Inventory Entry Update Actions', () => {
})

test('set expected delivery', async () => {
assert(inventoryEntry)
assert(inventoryEntry, 'inventory entry not created')
const expectedDelivery = '2021-04-02T15:06:19.700Z'
const response = await supertest(ctMock.app)
.post(`/dummy/inventory/${inventoryEntry.id}`)
Expand All @@ -131,8 +131,8 @@ describe('Inventory Entry Update Actions', () => {
})

test('set custom field', async () => {
assert(inventoryEntry)
assert(customType)
assert(inventoryEntry, 'inventory entry not created')
assert(customType, 'custom type not created')

const setCustomTypeResponse = await supertest(ctMock.app)
.post(`/dummy/inventory/${inventoryEntry.id}`)
Expand Down Expand Up @@ -160,4 +160,17 @@ describe('Inventory Entry Update Actions', () => {
expect(response.body.version).toBe(3)
expect(response.body.custom.fields['foo']).toBe('bar')
})

test('set restockable in days', async () => {
assert(inventoryEntry, 'inventory entry not created')
const response = await supertest(ctMock.app)
.post(`/dummy/inventory/${inventoryEntry.id}`)
.send({
version: 1,
actions: [{ action: 'setRestockableInDays', restockableInDays: 0 }],
})
expect(response.status).toEqual(200)
expect(response.body.version).toEqual(2)
expect(response.body.restockableInDays).toEqual(0)
})
})
118 changes: 110 additions & 8 deletions src/services/order.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import assert from 'assert'
import { Order } from '@commercetools/platform-sdk'
import { Order, Payment, State } from '@commercetools/platform-sdk'
import supertest from 'supertest'
import { CommercetoolsMock } from '../index'
import { CommercetoolsMock, getBaseResourceProperties } from '../index'

describe('Order Query', () => {
const ctMock = new CommercetoolsMock()
Expand Down Expand Up @@ -42,7 +42,7 @@ describe('Order Query', () => {
})

test('no filter', async () => {
assert(order)
assert(order, 'order not created')

const response = await supertest(ctMock.app).get(`/dummy/orders`)
expect(response.status).toBe(200)
Expand All @@ -53,7 +53,7 @@ describe('Order Query', () => {
})

test('filter orderNumber', async () => {
assert(order)
assert(order, 'order not created')

{
const response = await supertest(ctMock.app)
Expand All @@ -70,6 +70,108 @@ describe('Order Query', () => {
expect(response.body.count).toBe(1)
}
})

test('expand payment without payments', async () => {
assert(order, 'order not created')

const response = await supertest(ctMock.app)
.get(`/dummy/orders/${order.id}`)
.query({ expand: 'paymentInfo.payments[*].paymentStatus.state' })

expect(response.status).toBe(200)
expect(response.body.id).toBe(order.id)
})
})

describe('Order expand', () => {
const ctMock = new CommercetoolsMock({
defaultProjectKey: 'dummy',
})

test('expand payment states', async () => {
const state: State = {
...getBaseResourceProperties(),
builtIn: false,
initial: false,
key: 'PaymentSuccess',
type: 'PaymentState',
}

const payment: Payment = {
...getBaseResourceProperties(),
interfaceInteractions: [],
paymentStatus: {
state: {
typeId: 'state',
id: state.id,
},
},
amountPlanned: {
type: 'centPrecision',
fractionDigits: 2,
centAmount: 1234,
currencyCode: 'EUR',
},
paymentMethodInfo: {
paymentInterface: 'buckaroo',
method: 'mastercard',
},
version: 2,
transactions: [
{
id: 'fake-transaction-id',
type: 'Charge',
amount: {
centAmount: 1234,
currencyCode: 'EUR',
type: 'centPrecision',
fractionDigits: 2,
},
state: 'Success',
},
],
}

const order: Order = {
customLineItems: [],
lastMessageSequenceNumber: 0,
lineItems: [],
orderState: 'Open',
origin: 'Customer',
refusedGifts: [],
syncInfo: [],
totalPrice: {
type: 'centPrecision',
fractionDigits: 2,
centAmount: 2000,
currencyCode: 'EUR',
},
...getBaseResourceProperties(),
orderNumber: '1337',
paymentInfo: {
payments: [
{
typeId: 'payment',
id: payment.id,
},
],
},
}

ctMock.project().add('state', state)
ctMock.project().add('payment', payment)
ctMock.project().add('order', order)

const response = await supertest(ctMock.app)
.get(`/dummy/orders/order-number=${order.orderNumber}`)
.query({ expand: 'paymentInfo.payments[*].paymentStatus.state' })

expect(response.status).toBe(200)
expect(response.body.id).toBe(order.id)
const maybePayment = response.body.paymentInfo.payments[0].obj
expect(maybePayment).toBeDefined()
expect(maybePayment.paymentStatus.state.obj).toBeDefined()
})
})

describe('Order Update Actions', () => {
Expand Down Expand Up @@ -98,7 +200,7 @@ describe('Order Update Actions', () => {
})

test('no update', async () => {
assert(order)
assert(order, 'order not created')

const response = await supertest(ctMock.app)
.post(`/dummy/orders/${order.id}`)
Expand All @@ -122,7 +224,7 @@ describe('Order Update Actions', () => {
})

test('setOrderNumber', async () => {
assert(order)
assert(order, 'order not created')

const response = await supertest(ctMock.app)
.post(`/dummy/orders/${order.id}`)
Expand All @@ -136,7 +238,7 @@ describe('Order Update Actions', () => {
})

test('changeOrderState', async () => {
assert(order)
assert(order, 'order not created')

const response = await supertest(ctMock.app)
.post(`/dummy/orders/${order.id}`)
Expand All @@ -150,7 +252,7 @@ describe('Order Update Actions', () => {
})

test('changePaymentState | changeOrderState', async () => {
assert(order)
assert(order, 'order not created')

const response = await supertest(ctMock.app)
.post(`/dummy/orders/${order.id}`)
Expand Down
3 changes: 2 additions & 1 deletion src/services/order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ export class OrderService extends AbstractService {
getWithOrderNumber(request: Request, response: Response) {
const resource = this.repository.getWithOrderNumber(
request.params.projectKey,
request.params.orderNumber
request.params.orderNumber,
request.query
)
if (resource) {
return response.status(200).send(resource)
Expand Down
8 changes: 4 additions & 4 deletions src/services/product.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ describe('Product update actions', () => {
})

test('setAttribute masterVariant', async () => {
assert(product)
assert(product, 'product not created')

const response = await supertest(ctMock.app)
.post(`/dummy/products/${product.id}`)
Expand All @@ -118,7 +118,7 @@ describe('Product update actions', () => {
})

test('setAttribute variant', async () => {
assert(product)
assert(product, 'product not created')

const response = await supertest(ctMock.app)
.post(`/dummy/products/${product.id}`)
Expand All @@ -138,7 +138,7 @@ describe('Product update actions', () => {
})

test('setAttribute variant and publish', async () => {
assert(product)
assert(product, 'product not created')

const response = await supertest(ctMock.app)
.post(`/dummy/products/${product.id}`)
Expand All @@ -159,7 +159,7 @@ describe('Product update actions', () => {
})

test('setAttribute overwrite', async () => {
assert(product)
assert(product, 'product not created')

const response = await supertest(ctMock.app)
.post(`/dummy/products/${product.id}`)
Expand Down
2 changes: 1 addition & 1 deletion src/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ export class InMemoryStorage extends AbstractStorage {
this.forProjectKey(projectKey)[typeId]?.set(obj.id, obj)

const resource = this.get(projectKey, typeId, obj.id, params)
assert(resource)
assert(resource, `resource of type ${typeId} with id ${obj.id} not created`)
return resource
}

Expand Down

0 comments on commit fcf5dbe

Please sign in to comment.