Skip to content

Commit

Permalink
feat(medusa): cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
adrien2p committed Aug 5, 2022
1 parent c908e7f commit 52636bf
Show file tree
Hide file tree
Showing 7 changed files with 301 additions and 800 deletions.
16 changes: 12 additions & 4 deletions packages/medusa/src/interfaces/payment-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import {
PaymentSession,
PaymentSessionStatus,
} from "../models"
import { PaymentService } from "medusa-interfaces"

export type Data = Record<string, unknown>
export type PaymentData = Data
export type PaymentSessionData = Data

export interface PaymentService {
export interface PaymentService<T extends TransactionBaseService<never>>
extends TransactionBaseService<T> {
getIdentifier(): string

getPaymentData(paymentSession: PaymentSession): Promise<PaymentData>
Expand Down Expand Up @@ -50,9 +52,11 @@ export interface PaymentService {
): Promise<PaymentSessionStatus>
}

export abstract class AbstractPaymentService
extends TransactionBaseService<AbstractPaymentService>
implements PaymentService
export abstract class AbstractPaymentService<
T extends TransactionBaseService<never>
>
extends TransactionBaseService<T>
implements PaymentService<T>
{
protected constructor(container: unknown, config?: Record<string, unknown>) {
super(container, config)
Expand Down Expand Up @@ -110,3 +114,7 @@ export abstract class AbstractPaymentService
paymentSessionData: PaymentSessionData
): Promise<PaymentSessionStatus>
}

export function isPaymentService(obj: unknown): boolean {
return obj instanceof AbstractPaymentService || obj instanceof PaymentService
}
9 changes: 2 additions & 7 deletions packages/medusa/src/loaders/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,15 @@ import {
FileService,
FulfillmentService,
OauthService,
PaymentService,
} from "medusa-interfaces"
import path from "path"
import { EntitySchema } from "typeorm"
import {
AbstractFileService,
AbstractPaymentService,
AbstractTaxService,
isBatchJobStrategy,
isFileService,
isNotificationService,
isPaymentService,
isPriceSelectionStrategy,
isSearchService,
isTaxCalculationStrategy,
Expand Down Expand Up @@ -351,10 +349,7 @@ export async function registerServices(
throw new Error(message)
}

if (
loaded.prototype instanceof AbstractPaymentService ||
loaded.prototype instanceof PaymentService
) {
if (isPaymentService(loaded.prototype)) {
// Register our payment providers to paymentProviders
container.registerAdd(
"paymentProviders",
Expand Down
2 changes: 1 addition & 1 deletion packages/medusa/src/models/cart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ export class Cart extends SoftDeletableEntity {

@OneToOne(() => Payment)
@JoinColumn({ name: "payment_id" })
payment?: Payment
payment: Payment

@OneToMany(() => ShippingMethod, (method) => method.cart, {
cascade: ["soft-remove", "remove"],
Expand Down
157 changes: 0 additions & 157 deletions packages/medusa/src/services/__mocks__/test-pay-legacy.js

This file was deleted.

119 changes: 4 additions & 115 deletions packages/medusa/src/services/__mocks__/test-pay.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/*
const testPayService = {
export const testPayServiceMock = {
identifier: "test-pay",
getIdentifier: "test-pay",
getStatus: jest.fn().mockResolvedValue(Promise.resolve("authorised")),
Expand Down Expand Up @@ -34,118 +33,8 @@ const testPayService = {
})
}

export default testPayService*/

import { AbstractPaymentService } from "../../interfaces"

class TestPayService extends AbstractPaymentService {
static identifier = "test-pay"

getIdentifier() {
return TestPayService.identifier
}

constructor(...args) {
super(...args)
}

async getStatus(paymentData) {
return "authorized"
}

async retrieveSavedMethods(customer) {
return Promise.resolve([])
}

async createPayment(cart) {
return undefined
}

async retrievePayment(data) {
return {}
}

async getPaymentData(sessionData) {
return {}
}

async authorizePayment(sessionData, context = {}) {
return { data: {}, status: "authorized" }
}

async updatePaymentData(sessionData, update) {
return {}
}

async updatePayment(sessionData, cart) {
return {}
}

async deletePayment(payment) {
return {}
}

async capturePayment(payment) {
return {}
}

async refundPayment(payment, amountToRefund) {
return {}
}

async cancelPayment(payment) {
return {}
}
}

TestPayService.prototype.createPayment = jest.fn().mockImplementation((cart) => {
const fields = [
"total",
"subtotal",
"tax_total",
"discount_total",
"shipping_total",
"gift_card_total",
]

const data = {}
for (const k of fields) {
data[k] = cart[k]
}

return data
})

TestPayService.prototype.updatePayment = jest.fn().mockImplementation(() => {
return {}
})

TestPayService.prototype.deletePayment = jest.fn().mockImplementation(() => {
return {}
})

TestPayService.prototype.deletePayment = jest.fn().mockImplementation(() => {
return {}
})

TestPayService.prototype.authorizePayment = jest.fn().mockImplementation(() => {
return {}
})

TestPayService.prototype.updatePaymentData = jest.fn().mockImplementation(() => {
return {}
})

TestPayService.prototype.cancelPayment = jest.fn().mockImplementation(() => {
return {}
})

TestPayService.prototype.capturePayment = jest.fn().mockImplementation(() => {
return {}
})

TestPayService.prototype.refundPayment = jest.fn().mockImplementation(() => {
return {}
const mock = jest.fn().mockImplementation(() => {
return testPayServiceMock
})

export default TestPayService
export default mock
Loading

0 comments on commit 52636bf

Please sign in to comment.