-
Notifications
You must be signed in to change notification settings - Fork 102
feat(pos): merchants services #3528
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
lengyel-arpad85
merged 7 commits into
pos-card-services
from
al/raf-1084-merchants-services
Jul 10, 2025
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9b34f42
merchant service
lengyel-arpad85 5cd8be1
jest setup
lengyel-arpad85 0c0ae16
tests
lengyel-arpad85 0743940
cleanup & format
lengyel-arpad85 daf4566
unused var
lengyel-arpad85 bbf2fa2
jest cleanup
lengyel-arpad85 725ca58
removed unused vars
lengyel-arpad85 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| 'use strict' | ||
| // eslint-disable-next-line @typescript-eslint/no-var-requires | ||
| const baseConfig = require('../../jest.config.base.js') | ||
| // eslint-disable-next-line @typescript-eslint/no-var-requires | ||
| const packageName = require('./package.json').name | ||
|
|
||
| module.exports = { | ||
| ...baseConfig, | ||
| clearMocks: true, | ||
| testTimeout: 30000, | ||
| roots: [`<rootDir>/packages/${packageName}`], | ||
| setupFiles: [`<rootDir>/packages/${packageName}/jest.env.js`], | ||
| globalSetup: `<rootDir>/packages/${packageName}/jest.setup.ts`, | ||
| globalTeardown: `<rootDir>/packages/${packageName}/jest.teardown.js`, | ||
| testRegex: `(packages/${packageName}/.*/__tests__/.*|\\.(test|spec))\\.tsx?$`, | ||
| testEnvironment: `<rootDir>/packages/${packageName}/jest.custom-environment.ts`, | ||
| moduleDirectories: [ | ||
| `node_modules`, | ||
| `packages/${packageName}/node_modules`, | ||
| `<rootDir>/node_modules` | ||
| ], | ||
| modulePaths: [ | ||
| `node_modules`, | ||
| `<rootDir>/packages/${packageName}/src/`, | ||
| `<rootDir>/node_modules` | ||
| ], | ||
| id: packageName, | ||
| displayName: packageName, | ||
| rootDir: '../..' | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| import { TestEnvironment } from 'jest-environment-node' | ||
| import nock from 'nock' | ||
|
|
||
| export default class CustomEnvironment extends TestEnvironment { | ||
| constructor(config, context) { | ||
| super(config, context) | ||
| this.global.nock = nock | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| process.env.LOG_LEVEL = 'silent' | ||
| process.env.INSTANCE_NAME = 'Rafiki' | ||
| process.env.KEY_ID = 'myKey' | ||
| process.env.OPEN_PAYMENTS_URL = 'http://127.0.0.1:3000' | ||
| process.env.ILP_CONNECTOR_URL = 'http://127.0.0.1:3002' | ||
| process.env.ILP_ADDRESS = 'test.rafiki' | ||
| process.env.AUTH_SERVER_GRANT_URL = 'http://127.0.0.1:3006' | ||
| process.env.AUTH_SERVER_INTROSPECTION_URL = 'http://127.0.0.1:3007/' | ||
| process.env.AUTH_SERVICE_API_URL = 'http://127.0.0.1:3011' | ||
| process.env.WEBHOOK_URL = 'http://127.0.0.1:4001/webhook' | ||
| process.env.STREAM_SECRET = '2/PxuRFV9PAp0yJlnAifJ+1OxujjjI16lN+DBnLNRLA=' | ||
| process.env.USE_TIGERBEETLE = false | ||
| process.env.ENABLE_TELEMETRY = false | ||
| process.env.AUTH_ADMIN_API_URL = 'http://127.0.0.1:3003/graphql' | ||
| process.env.AUTH_ADMIN_API_SECRET = 'test-secret' | ||
| process.env.OPERATOR_TENANT_ID = 'cf5fd7d3-1eb1-4041-8e43-ba45747e9e5d' | ||
| process.env.API_SECRET = 'KQEXlZO65jUJXakXnLxGO7dk387mt71G9tZ42rULSNU=' | ||
| process.env.EXCHANGE_RATES_URL = 'http://example.com/rates' | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| import { knex } from 'knex' | ||
| import { GenericContainer, Wait } from 'testcontainers' | ||
| require('./jest.env') // set environment variables | ||
|
|
||
| const POSTGRES_PORT = 5432 | ||
| const REDIS_PORT = 6379 | ||
lengyel-arpad85 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| const setup = async (globalConfig): Promise<void> => { | ||
| const workers = globalConfig.maxWorkers | ||
|
|
||
| const setupDatabase = async () => { | ||
| if (!process.env.DATABASE_URL) { | ||
| const postgresContainer = await new GenericContainer('postgres:15') | ||
| .withExposedPorts(POSTGRES_PORT) | ||
| .withBindMounts([ | ||
| { | ||
| source: __dirname + '/scripts/init.sh', | ||
| target: '/docker-entrypoint-initdb.d/init.sh' | ||
| } | ||
| ]) | ||
| .withEnvironment({ | ||
| POSTGRES_PASSWORD: 'password' | ||
| }) | ||
| .withHealthCheck({ | ||
| test: ['CMD-SHELL', 'pg_isready -d testing'], | ||
| interval: 10000, | ||
| timeout: 5000, | ||
| retries: 5 | ||
| }) | ||
| .withWaitStrategy(Wait.forHealthCheck()) | ||
| .start() | ||
|
|
||
| process.env.DATABASE_URL = `postgresql://postgres:password@localhost:${postgresContainer.getMappedPort( | ||
| POSTGRES_PORT | ||
| )}/testing` | ||
|
|
||
| global.__BACKEND_POSTGRES__ = postgresContainer | ||
lengyel-arpad85 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| const db = knex({ | ||
| client: 'postgresql', | ||
| connection: process.env.DATABASE_URL, | ||
| pool: { | ||
| min: 2, | ||
| max: 10 | ||
| }, | ||
| migrations: { | ||
| tableName: 'pos_knex_migrations' | ||
| } | ||
| }) | ||
|
|
||
| // node pg defaults to returning bigint as string. This ensures it parses to bigint | ||
| db.client.driver.types.setTypeParser( | ||
| db.client.driver.types.builtins.INT8, | ||
| 'text', | ||
| BigInt | ||
| ) | ||
| await db.migrate.latest({ | ||
| directory: __dirname + '/migrations' | ||
| }) | ||
|
|
||
| for (let i = 1; i <= workers; i++) { | ||
| const workerDatabaseName = `testing_${i}` | ||
|
|
||
| await db.raw(`DROP DATABASE IF EXISTS ${workerDatabaseName}`) | ||
| await db.raw(`CREATE DATABASE ${workerDatabaseName} TEMPLATE testing`) | ||
| } | ||
|
|
||
| global.__BACKEND_KNEX__ = db | ||
| } | ||
|
|
||
| const setupRedis = async () => { | ||
lengyel-arpad85 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (!process.env.REDIS_URL) { | ||
| const redisContainer = await new GenericContainer('redis:7') | ||
| .withExposedPorts(REDIS_PORT) | ||
| .start() | ||
|
|
||
| global.__BACKEND_REDIS__ = redisContainer | ||
| process.env.REDIS_URL = `redis://localhost:${redisContainer.getMappedPort( | ||
| REDIS_PORT | ||
| )}` | ||
| } | ||
| } | ||
|
|
||
| await Promise.all([setupDatabase(), setupRedis()]) | ||
| } | ||
|
|
||
| export default setup | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| module.exports = async () => { | ||
| await global.__BACKEND_KNEX__.migrate.rollback( | ||
lengyel-arpad85 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { directory: __dirname + '/migrations' }, | ||
| true | ||
| ) | ||
| await global.__BACKEND_KNEX__.destroy() | ||
| if (global.__BACKEND_POSTGRES__) { | ||
| await global.__BACKEND_POSTGRES__.stop() | ||
| } | ||
| if (global.__BACKEND_REDIS__) { | ||
lengyel-arpad85 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| await global.__BACKEND_REDIS__.stop() | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import { startTigerBeetleContainer } from './src/tests/tigerbeetle' | ||
lengyel-arpad85 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| import { StartedTestContainer } from 'testcontainers' | ||
|
|
||
| import CustomTestEnvironment from './jest.custom-environment' | ||
|
|
||
| export default class TigerBeetleEnvironment extends CustomTestEnvironment { | ||
| private tbContainer: StartedTestContainer | undefined | ||
|
|
||
| public async setup(): Promise<void> { | ||
| await super.setup() | ||
| const tbContainer = await startTigerBeetleContainer() | ||
|
|
||
| this.tbContainer = tbContainer.container | ||
| this.global.tigerBeetlePort = tbContainer.port | ||
| } | ||
|
|
||
| public async teardown(): Promise<void> { | ||
| await super.teardown() | ||
| await this.tbContainer?.stop() | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| #!/bin/bash | ||
| set -e | ||
|
|
||
| psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL | ||
| DROP DATABASE IF EXISTS TESTING; | ||
| CREATE DATABASE testing; | ||
| CREATE DATABASE development; | ||
| EOSQL |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| import { IocContract } from '@adonisjs/fold' | ||
|
|
||
| import { Merchant } from './model' | ||
| import { MerchantService } from './service' | ||
|
|
||
| import { createTestApp, TestContainer } from '../tests/app' | ||
| import { truncateTables } from '../tests/tableManager' | ||
| import { Config } from '../config/app' | ||
|
|
||
| import { initIocContainer } from '../' | ||
| import { AppServices } from '../app' | ||
|
|
||
| describe('Merchant Service', (): void => { | ||
| let deps: IocContract<AppServices> | ||
| let appContainer: TestContainer | ||
| let merchantService: MerchantService | ||
|
|
||
| beforeAll(async (): Promise<void> => { | ||
| deps = initIocContainer({ | ||
| ...Config | ||
| }) | ||
|
|
||
| appContainer = await createTestApp(deps) | ||
| merchantService = await deps.use('merchantService') | ||
| }) | ||
|
|
||
| afterEach(async (): Promise<void> => { | ||
| jest.useRealTimers() | ||
| await truncateTables(deps) | ||
| }) | ||
|
|
||
| afterAll(async (): Promise<void> => { | ||
| await appContainer.shutdown() | ||
| }) | ||
|
|
||
| describe('create', (): void => { | ||
| test('creates a merchant', async (): Promise<void> => { | ||
| const merchant = await merchantService.create('Test merchant') | ||
| expect(merchant).toEqual({ id: merchant.id, name: 'Test merchant' }) | ||
| }) | ||
| }) | ||
|
|
||
| describe('delete', (): void => { | ||
| test('soft deletes an existing merchant', async (): Promise<void> => { | ||
| const created = await merchantService.create('Test merchant') | ||
|
|
||
| const result = await merchantService.delete(created.id) | ||
| expect(result).toBe(true) | ||
|
|
||
| const deletedMerchant = await Merchant.query() | ||
| .findById(created.id) | ||
| .whereNotNull('deletedAt') | ||
| expect(deletedMerchant).toBeDefined() | ||
| expect(deletedMerchant?.deletedAt).toBeDefined() | ||
| }) | ||
|
|
||
| test('returns false for already deleted merchant', async (): Promise<void> => { | ||
| const created = await merchantService.create('Test merchant') | ||
|
|
||
| await merchantService.delete(created.id) | ||
| const secondDelete = await merchantService.delete(created.id) | ||
| expect(secondDelete).toBe(false) | ||
| }) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| import { BaseService } from '../shared/baseService' | ||
| import { TransactionOrKnex } from 'objection' | ||
| import { Merchant } from './model' | ||
|
|
||
| export interface MerchantService { | ||
| create(name: string): Promise<Merchant> | ||
| delete(id: string): Promise<boolean> | ||
| } | ||
|
|
||
| interface ServiceDependencies extends BaseService { | ||
| knex: TransactionOrKnex | ||
| } | ||
|
|
||
| export async function createMerchantService({ | ||
| logger, | ||
| knex | ||
| }: ServiceDependencies): Promise<MerchantService> { | ||
| const log = logger.child({ | ||
| service: 'MerchantService' | ||
| }) | ||
| const deps: ServiceDependencies = { | ||
| logger: log, | ||
| knex | ||
| } | ||
|
|
||
| return { | ||
| create: (input: string) => createMerchant(deps, input), | ||
| delete: (id: string) => deleteMerchant(deps, id) | ||
| } | ||
| } | ||
|
|
||
| async function createMerchant( | ||
| deps: ServiceDependencies, | ||
| name: string | ||
| ): Promise<Merchant> { | ||
| return await Merchant.query(deps.knex).insert({ name }) | ||
| } | ||
|
|
||
| async function deleteMerchant( | ||
| deps: ServiceDependencies, | ||
| id: string | ||
| ): Promise<boolean> { | ||
| const deleted = await Merchant.query(deps.knex) | ||
| .patch({ deletedAt: new Date() }) | ||
| .whereNull('deletedAt') | ||
| .where('id', id) | ||
| return deleted > 0 | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.