From 8819cf9c67e33c76cbac65a9ca2b6ff86786d251 Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Sat, 30 Mar 2024 14:22:59 +0100 Subject: [node] fixed contracting and implemented renegotiation --- nodejs/lib/contracts.ts | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) (limited to 'nodejs/lib/contracts.ts') diff --git a/nodejs/lib/contracts.ts b/nodejs/lib/contracts.ts index 54347cb..2b6f877 100644 --- a/nodejs/lib/contracts.ts +++ b/nodejs/lib/contracts.ts @@ -22,16 +22,29 @@ export async function accept(contract: Contract): Promise { return response.data.contract; } -export async function contracts(): Promise> { +export async function getContracts(): Promise> { const response = await api.sendPaginated({endpoint: '/my/contracts'}); response.forEach(contract => dbContracts.setContract(contract)); return response; } +export async function getContract(contract: Contract): Promise { + try { + return dbContracts.getContract(contract.id); + } catch {} + const response = await api.send({endpoint: `/my/contracts/${contract.id}`}); + if (response.error) { + api.debugLog(response); + throw response; + } + dbContracts.setContract(response.data); + return response.data; +} + export async function deliver(contract: Contract, ship: Ship): Promise { contract = dbContracts.getContract(contract.id); ship = dbShips.getShip(ship.symbol); - if (contract.terms.deliver[0].unitsRequired >= contract.terms.deliver[0].unitsFulfilled) { + if (contract.terms.deliver[0].unitsRequired <= contract.terms.deliver[0].unitsFulfilled) { return await fulfill(contract); } const tradeSymbol = contract.terms.deliver[0].tradeSymbol; @@ -54,7 +67,7 @@ export async function deliver(contract: Contract, ship: Ship): Promise } dbContracts.setContract(response.data.contract); dbShips.setShipCargo(ship.symbol, response.data.cargo); - if(response.data.contract.terms.deliver[0].unitsRequired >= response.data.contract.terms.deliver[0].unitsFulfilled) { + if(response.data.contract.terms.deliver[0].unitsRequired <= response.data.contract.terms.deliver[0].unitsFulfilled) { return await fulfill(response.data.contract); } return response.data.contract; -- cgit v1.2.3