From 6504e44ffa97965e47e893b55621d2d04003d519 Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Wed, 15 May 2024 23:49:33 +0200 Subject: [node] Added agent class, and fixed contract updates --- nodejs/lib/contracts.ts | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) (limited to 'nodejs/lib/contracts.ts') diff --git a/nodejs/lib/contracts.ts b/nodejs/lib/contracts.ts index 1b54178..009c853 100644 --- a/nodejs/lib/contracts.ts +++ b/nodejs/lib/contracts.ts @@ -1,5 +1,4 @@ import { - Agent, Cargo, } from './types.ts'; import { @@ -8,8 +7,8 @@ import { send, sendPaginated, } from './api.ts'; +import { Agent, setAgent } from './agent.ts'; import { Ship } from './ships.ts'; -import * as dbAgents from '../database/agents.ts'; export async function getContracts(): Promise> { const response = await sendPaginated({endpoint: '/my/contracts'}); @@ -54,7 +53,9 @@ export class Contract { debugLog(response); throw response; } - dbAgents.setAgent(response.data.agent); + this.accepted = contract.accepted; + this.terms = contract.terms; + setAgent(response.data.agent); } async deliver(ship: Ship): Promise { const unitsRemaining = this.terms.deliver[0].unitsRequired - this.terms.deliver[0].unitsFulfilled; @@ -83,19 +84,22 @@ export class Contract { throw response; } } + this.terms = contract.terms; ship.cargo = response.data.cargo; if(response.data.contract.terms.deliver[0].unitsRequired <= response.data.contract.terms.deliver[0].unitsFulfilled) { return await this.fulfill(); } } async fulfill(): Promise { - if (this.terms.deliver[0].unitsRequired > this.terms.deliver[0].unitsFulfilled) return; + if (this.terms.deliver[0].unitsRequired < this.terms.deliver[0].unitsFulfilled) return; if (this.fulfilled) return; const response = await send<{agent: Agent, contract: Contract}>({ endpoint: `/my/contracts/${this.id}/fulfill`, method: 'POST'}); if (response.error) { debugLog(response); throw response; } - dbAgents.setAgent(response.data.agent); + setAgent(response.data.agent); + this.fulfilled = true; + this.terms = contract.terms; } }; -- cgit v1.2.3