@lafourche/pvx-node-client
TypeScript icon, indicating that this package has built-in type declarations

1.4.0 • Public • Published

PeopleVox client

The client is related to PVX and is implemented in Typescript

Get started

npm install @lafourche/pvx-node-client

Basic Usage

Init client

To init the pvx client, you will need a credentials and a wsdl url. The client will not try to authenticate until we execute a request.

import { PvxClient } from '@lafourche/pvx-node-client';

const credentials = {
  clientId: '<your-client-id>',
  username: '<your-username-id>',
  password: '<your-password>',
};

const wsdlUrl: 'https://qac.peoplevox.net/<your-client-id>/resources/integrationservicev4.asmx?wsdl';

const pvxClient = new PvxClient(credentials, wsdlUrl);

you can pass additional parameters to constructor.

Custom Logger

It must implement the following interface:

interface LoggerInterface {
  info(message: string): void;
  warn(message: string): void;
  error(message: string): void;
}

Custom Transformer

We created a transformer which roles is to convert csv raw data get from pvx to an AbstractModel. Transformer must implement the following interface:

interface TransformerInterface {
  toCSV<T extends AbstractModel>(model: T): string;
  fromCSV<T extends AbstractModel>(
    className: Constructor<T>,
    csv: string,
  ): Promise<T[]>;
}

Model

To manipulate some client method (getData, getReportData), you will need to use pre-defined Model (Item, Sale, SaleItem...) or defined a custom one by extending AbstractModel.

Structure of a Custom Model

A model must extend AbstractModel and use pre-defined Decorator (Resource and Field) to configure it.

Resource Decorator

Corresponds to Data Type defined in PVX Integration. It map ResourceName in PVX and Classname.

import { Resource } from '@lafourche/pvx-node-client';
// Resource name different with classname
@Resource('Item Type')
class Item {
  // ...
}

// The name of the resource will have the name of the class
@Resource()
class ItemInventoryReport {
  // ...
}

Field Decorator

Help us to map the serialized name with a class attribute.

import { Field } from '@lafourche/pvx-node-client';

...
class Item {
 @Field("Item Code")
 public code;

 @Field() // Field will be serialized as Barcode if not specified
 public Barcode;
}

Example of custom model

import { Field, Resource } from '@lafourche/pvx-node-client';

@Resource('Item Type')
class Item {
  @Field('Item code')
  public itemCode?: string;
  @Field('Name')
  public name?: string;
  @Field('Weight')
  public weight: number;
}

Get Data

getData is a client method for retrieving data.

In next example. We will fetch Item (A model defined in library).

import { PvxClient, Item } from '@lafourche/pvx-node-client';

class ItemRepository {
  private client: PvxClient;

  constructor(client: PvxClient) {
    this.client = client;
  }
  // ...
  async getItem(itemCode: string): Promise<Item | null> {
    const results = await this.client.getData(
      Item, // The model class here is used to retrieve the resource name
      1,
      1,
      `itemCode.Equals("${itemCode}")`,
    );

    if (results.length === 0) {
      return null;
    }

    return results[0];
  }
  // ...
}

Get Report Data

getReportData is a client method for retrieving data from Pvx system report or custom report.

In next example. We will fetch Item inventory summary (A system report from PVX).

import { PvxClient } from '@lafourche/pvx-node-client';

class InventoryReport {
  private client: PvxClient;

  constructor(client: PvxClient) {
    this.client = client;
  }
  // ...
  async getItemInventory(
    itemCode: string,
    site: string,
  ): Promise<ItemIventory[] | null> {
    const results = await this.client.getReportData(
      ItemIventory, // The model class here is used to retrieve the resource name
      1,
      10,
      `[Item code].Equals("${itemCode}") AND [Site reference].Equals("${site}")`,
    );

    if (results.length === 0) {
      return null;
    }

    return results;
  }
  // ...
}

Save Data

For saving a data, use the method saveData.

import { PvxClient, Item, Action } from '@lafourche/pvx-node-client';

class ItemRepository {
  private client: PvxClient;

  constructor(client: PvxClient) {
    this.client = client;
  }

  async addItem(item: Item): Promise<void> {
    await this.client.saveData(item, Action.NO);
  }
}

Subscribe to an event

Event covered by this client:

export enum EventType {
  AVAILABILITY_CHANGES_EVENT = 'AvailabilityChanges',
  SALES_ORDER_STATUS_CHANGES_EVENT = 'SalesOrderStatusChanges',
  GOODS_RECEIVED_EVENT = 'GoodsReceived',
  TRACKING_NUMBER_RECEIVED_EVENT = 'TrackingNumberReceived',
  INCREMENTAL_CHANGES_EVENT = 'IncrementalChanges',
  RETURNS_EVENT = 'Returns',
  DESPATCH_PACKAGE_TRACKING_NUMBER_RECEIVED_EVENT = 'DespatchPackageTrackingNumberReceived',
  DESPATCH_PACKAGE_DESPATCHED_EVENT = 'DespatchPackageDespatched',
}

In PVX we have 3 types of subscription and each one returns an integer.

Subscribe to post event

The endpoint url will be called with POST Method. Documentation

const pvxSubscriptionId = await client.subscribePostEvent(
  EventType.AVAILABILITY_CHANGES_EVENT,
  'https://endpointurl',
);

The are optional parameters:

  • postParams: string
  • filter: string
  • encodeParameterData: boolean

Subscribe to an get event

const pvxSubscriptionId = await client.SubscribeEvent(
  EventType.AVAILABILITY_CHANGES_EVENT,
  'https://myUrl/inventory/update?available={Available}&amp;inventoryItemId={Attribute3}',
);

The are optional parameters:

  • filter: string
  • encodeParameterData: boolean

See documentation for more information.

Subscribe to an event with filter on site

const pvxSubscriptionId = await client.SubscribeEventWithSitesFilters(
  EventType.AVAILABILITY_CHANGES_EVENT,
  'https://myUrl/inventory/update?available={Available}&amp;inventoryItemId={Attribute3}',
  'Site.Reference="mySite"',
);

The are optional parameters:

  • siteFilter: string
  • filter: string
  • encodeParameterData: boolean

See documentation for more information.

Unsubscribe to an event

To unsubscribe you need the id of subscription.

const pvxSubscriptionId = 69;
await client.unsubscribeEvent(pvxSubscriptionId);

Package Sidebar

Install

npm i @lafourche/pvx-node-client

Weekly Downloads

0

Version

1.4.0

License

ISC

Unpacked Size

54.2 kB

Total Files

34

Last publish

Collaborators

  • isma94
  • julienthibeaut
  • skybot
  • martinlg