Skip to content

Commit

Permalink
add myPayment (#17)
Browse files Browse the repository at this point in the history
* add myPayment

* add addtransaction action
  • Loading branch information
BramKaashoek authored Jul 21, 2021
1 parent 92b0526 commit a7606a5
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,4 @@ Implement the following:
- New service in src/services
- Add new service to src/ctMock.ts ctMock.\_services
- Add new service to src/storage.ts InMemoryStorage
- Adjust src/types.ts RepositoryMap
- Adjust src/types.ts RepositoryMap and possibly serviceTypes
2 changes: 2 additions & 0 deletions src/ctMock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { DEFAULT_API_HOSTNAME, DEFAULT_AUTH_HOSTNAME } from './constants'
import { ProjectAPI } from './projectAPI'
import { copyHeaders } from './lib/proxy'
import { ProductService } from './services/product'
import { MyPaymentService } from './services/my-payment'

export type CommercetoolsMockOptions = {
validateCredentials: boolean
Expand Down Expand Up @@ -136,6 +137,7 @@ export class CommercetoolsMock {
),
order: new OrderService(projectRouter, this._storage),
payment: new PaymentService(projectRouter, this._storage),
'my-payment': new MyPaymentService(projectRouter, this._storage),
'shipping-method': new ShippingMethodService(
projectRouter,
this._storage
Expand Down
13 changes: 12 additions & 1 deletion src/repositories/payment.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
Payment,
PaymentAddTransactionAction,
PaymentDraft,
PaymentSetCustomFieldAction,
PaymentSetCustomTypeAction,
Expand Down Expand Up @@ -68,6 +69,7 @@ export class PaymentRepository extends AbstractRepository {
if (!resource.custom) {
throw new Error('Resource has no custom field')
}

resource.custom.fields[name] = value
},
setCustomType: (
Expand Down Expand Up @@ -95,8 +97,17 @@ export class PaymentRepository extends AbstractRepository {
}
}
},
addTransaction: (
projectKey: string,
resource: Writable<Payment>,
{ transaction }: PaymentAddTransactionAction
) => {
resource.transactions = [
...resource.transactions,
this.transactionFromTransactionDraft(transaction),
]
},
// addInterfaceInteraction: () => {},
// addTransaction: () => {},
// changeAmountPlanned: () => {},
// changeTransactionInteractionId: () => {},
// changeTransactionState: () => {},
Expand Down
77 changes: 77 additions & 0 deletions src/services/my-payment.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { MyPaymentDraft } from '@commercetools/platform-sdk'
import supertest from 'supertest'
import { CommercetoolsMock } from '../index'

const ctMock = new CommercetoolsMock()

describe('MyPayment', () => {
beforeEach(async () => {
const response = await supertest(ctMock.app)
.post('/dummy/types')
.send({
key: 'custom-payment',
name: {
'nl-NL': 'custom-payment',
},
resourceTypeIds: ['payment'],
})
expect(response.status).toBe(200)
})

test('Create payment', async () => {
const draft: MyPaymentDraft = {
amountPlanned: { currencyCode: 'EUR', centAmount: 1337 },
custom: {
type: { typeId: 'type', key: 'custom-payment' },
fields: {
foo: 'bar',
},
},
}
const response = await supertest(ctMock.app)
.post('/dummy/me/payments')
.send(draft)

expect(response.status).toBe(200)
expect(response.body).toEqual({
id: expect.anything(),
createdAt: expect.anything(),
lastModifiedAt: expect.anything(),
version: 1,
amountPlanned: {
type: 'centPrecision',
fractionDigits: 2,
currencyCode: 'EUR',
centAmount: 1337,
},
paymentStatus: {},
transactions: [],
interfaceInteractions: [],
custom: {
type: { typeId: 'type', id: expect.anything() },
fields: { foo: 'bar' },
},
})
})
test('Get payment', async () => {
const draft: MyPaymentDraft = {
amountPlanned: { currencyCode: 'EUR', centAmount: 1337 },
custom: {
type: { typeId: 'type', key: 'custom-payment' },
fields: {
foo: 'bar',
},
},
}
const createResponse = await supertest(ctMock.app)
.post('/dummy/me/payments')
.send(draft)

const response = await supertest(ctMock.app).get(
`/dummy/me/payments/${createResponse.body.id}`
)

expect(response.status).toBe(200)
expect(response.body).toEqual(createResponse.body)
})
})
17 changes: 17 additions & 0 deletions src/services/my-payment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import AbstractService from './abstract'
import { Router } from 'express'
import { AbstractStorage } from '../storage'
import { PaymentRepository } from '../repositories/payment'

export class MyPaymentService extends AbstractService {
public repository: PaymentRepository //TODO: MyPaymentRepository?

constructor(parent: Router, storage: AbstractStorage) {
super(parent)
this.repository = new PaymentRepository(storage)
}

getBasePath() {
return 'me/payments'
}
}
4 changes: 3 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ import { TaxCategoryRepository } from './repositories/tax-category'

export type Writable<T> = { -readonly [P in keyof T]: Writable<T[P]> }

type serviceTypes = ReferenceTypeId | 'my-payment'

export type Services = Partial<
{
[index in ReferenceTypeId]: AbstractService
[index in serviceTypes]: AbstractService
}
>

Expand Down

0 comments on commit a7606a5

Please sign in to comment.