From 1daa79efb6b692abd25298d1154f077b475f5093 Mon Sep 17 00:00:00 2001 From: Robert Seitz Date: Mon, 15 Apr 2024 15:45:43 -0400 Subject: [PATCH] text changing index --- lib/repositories/base.ts | 11 ++++++++ lib/repositories/generic-orders-repository.ts | 20 +++++++++++++- .../repositories/dynamo-repository.test.ts | 27 +++++++++++++++++++ 3 files changed, 57 insertions(+), 1 deletion(-) diff --git a/lib/repositories/base.ts b/lib/repositories/base.ts index a428fb99..795180cc 100644 --- a/lib/repositories/base.ts +++ b/lib/repositories/base.ts @@ -41,5 +41,16 @@ export interface BaseOrdersRepository { txHash?: string, settledAmounts?: SettledAmount[] ) => Promise + updateOrder: (orderHash: string, entity: T) => Promise deleteOrders: (orderHashes: string[]) => Promise + queryOrderEntity( + partitionKey: string | number, + index: string, + limit: number | undefined, + cursor?: string, + sortKey?: SORT_FIELDS | undefined, + sort?: string | undefined, // ex gt(123) + desc?: boolean, + filters?: { or: boolean; attr: string; eq: string }[] + ): Promise> } diff --git a/lib/repositories/generic-orders-repository.ts b/lib/repositories/generic-orders-repository.ts index b3c7a9aa..8939b588 100644 --- a/lib/repositories/generic-orders-repository.ts +++ b/lib/repositories/generic-orders-repository.ts @@ -197,7 +197,7 @@ export abstract class GenericOrdersRepository< } } - private async queryOrderEntity( + public async queryOrderEntity( partitionKey: string | number, index: string, limit: number | undefined, @@ -271,4 +271,22 @@ export abstract class GenericOrdersRepository< return lastEvaluatedKey } + + public async updateOrder(orderHash: string, entity: T): Promise { + try { + const order = checkDefined( + await this.getByHash(orderHash), + 'cannot find order by hash when updating order status' + ) + + await this.entity.update({ + ...order, //any missed fields + ...entity, + [TABLE_KEY.ORDER_HASH]: orderHash, + }) + } catch (e) { + log.error('updateOrderStatus error', { error: e }) + throw e + } + } } diff --git a/test/integ/repositories/dynamo-repository.test.ts b/test/integ/repositories/dynamo-repository.test.ts index 6b3748d8..f42e1a7f 100644 --- a/test/integ/repositories/dynamo-repository.test.ts +++ b/test/integ/repositories/dynamo-repository.test.ts @@ -1,9 +1,13 @@ import { DocumentClient } from 'aws-sdk/clients/dynamodb' +import { TABLE_KEY } from '../../../lib/config/dynamodb' import { ORDER_STATUS, SettledAmount, SORT_FIELDS, UniswapXOrderEntity } from '../../../lib/entities/Order' import { GetOrderTypeQueryParamEnum } from '../../../lib/handlers/get-orders/schema/GetOrderTypeQueryParamEnum' +import { DutchV1Order } from '../../../lib/models' import { DutchOrdersRepository } from '../../../lib/repositories/dutch-orders-repository' +import { ChainId } from '../../../lib/util/chain' import { generateRandomNonce } from '../../../lib/util/nonce' import { currentTimestampInSeconds } from '../../../lib/util/time' +import { SDKDutchOrderFactory } from '../../factories/SDKDutchOrderV1Factory' import { deleteAllRepoEntries } from './deleteAllRepoEntries' jest.mock('../../../lib/util/time') @@ -571,6 +575,29 @@ describe('OrdersRepository update status test', () => { }) }) + it.only('should successfully update index with orderType', async () => { + const newOrder = new DutchV1Order(SDKDutchOrderFactory.buildDutchOrder(), 'signature', ChainId.MAINNET) + await ordersRepository.putOrderAndUpdateNonceTransaction(newOrder.toEntity(ORDER_STATUS.OPEN)) + const firstResponse = await ordersRepository.getByHash(newOrder.inner.hash()) + + await ordersRepository.updateOrder(firstResponse!.orderHash, { + ...firstResponse, + chainId_orderStatus: `${firstResponse?.chainId}_${firstResponse?.orderStatus}_${firstResponse?.type}`, + } as any) + + const secondResponse = await ordersRepository.queryOrderEntity( + `${firstResponse?.chainId}_${firstResponse?.orderStatus}_${firstResponse?.type}`, //partitionKey: + `${TABLE_KEY.CHAIN_ID}_${TABLE_KEY.ORDER_STATUS}`, //index: + 5, //limit: + undefined, //cursor: + undefined, // sortKey: + undefined, //sort: + true //desc: + ) + expect(secondResponse.orders).toHaveLength(1) + expect(secondResponse.orders[0]).toEqual({ ...firstResponse, chainId_orderStatus: '1_open_Dutch' }) + }) + it('should throw error if order does not exist', async () => { await expect(ordersRepository.updateOrderStatus('nonexistent', ORDER_STATUS.FILLED)).rejects.toEqual( new Error('cannot find order by hash when updating order status')