How to use the @requestnetwork/types.RequestLogicTypes.STATE function in @requestnetwork/types

To help you get started, we’ve selected a few @requestnetwork/types examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github RequestNetwork / requestNetwork / packages / request-logic / src / actions / cancel.ts View on Github external
throw new Error('requestId must be given');
  }

  const signer: IdentityTypes.IIdentity = Action.getSignerIdentityFromAction(action);
  const signerRole = Request.getRoleInRequest(signer, request);

  // avoid to mutate the request
  let requestCopied: RequestLogicTypes.IRequest = Utils.deepCopy(request);
  requestCopied = Request.pushExtensionsData(requestCopied, action.data.parameters.extensionsData);
  requestCopied.events.push(generateEvent(action, timestamp, signer));

  if (signerRole === RequestLogicTypes.ROLE.PAYER) {
    if (request.state !== RequestLogicTypes.STATE.CREATED) {
      throw new Error('A payer cancel need to be done on a request with the state created');
    }
    requestCopied.state = RequestLogicTypes.STATE.CANCELED;
    return requestCopied;
  }

  if (signerRole === RequestLogicTypes.ROLE.PAYEE) {
    if (request.state === RequestLogicTypes.STATE.CANCELED) {
      throw new Error('Cannot cancel an already canceled request');
    }
    requestCopied.state = RequestLogicTypes.STATE.CANCELED;
    return requestCopied;
  }

  throw new Error('Signer must be the payer or the payee');
}
github RequestNetwork / requestNetwork / packages / request-logic / src / actions / increaseExpectedAmount.ts View on Github external
throw new Error('deltaAmount must be given');
  }
  if (!Utils.amount.isValid(action.data.parameters.deltaAmount)) {
    throw new Error('deltaAmount must be a string representing a positive integer');
  }

  const signer: IdentityTypes.IIdentity = Action.getSignerIdentityFromAction(action);
  const signerRole = Request.getRoleInRequest(signer, request);

  // avoid to mutate the request
  let requestCopied: RequestLogicTypes.IRequest = Utils.deepCopy(request);
  requestCopied = Request.pushExtensionsData(requestCopied, action.data.parameters.extensionsData);
  requestCopied.events.push(generateEvent(action, timestamp, signer));

  if (signerRole === RequestLogicTypes.ROLE.PAYER) {
    if (request.state === RequestLogicTypes.STATE.CANCELED) {
      throw new Error('the request must not be canceled');
    }
    // increase the expected amount and store it as string
    requestCopied.expectedAmount = Utils.amount.add(
      request.expectedAmount,
      action.data.parameters.deltaAmount,
    );

    return requestCopied;
  }

  throw new Error('signer must be the payer');
}
github RequestNetwork / requestNetwork / packages / request-logic / src / actions / accept.ts View on Github external
throw new Error('requestId must be given');
  }

  if (!request.payer) {
    throw new Error('the request must have a payer');
  }

  if (request.state !== RequestLogicTypes.STATE.CREATED) {
    throw new Error('the request state must be created');
  }

  const signer: IdentityTypes.IIdentity = Action.getSignerIdentityFromAction(action);
  const signerRole = Request.getRoleInRequest(signer, request);

  if (signerRole === RequestLogicTypes.ROLE.PAYER) {
    request.state = RequestLogicTypes.STATE.ACCEPTED;
  } else {
    throw new Error('Signer must be the payer');
  }
  // avoid to mutate the request
  let requestCopied: RequestLogicTypes.IRequest = Utils.deepCopy(request);
  requestCopied = Request.pushExtensionsData(requestCopied, action.data.parameters.extensionsData);
  requestCopied.events.push(generateEvent(action, timestamp, signer));

  return requestCopied;
}
github RequestNetwork / requestNetwork / packages / request-logic / src / actions / create.ts View on Github external
request.version = Action.getVersionFromAction(action);
  request.events = [generateEvent(action, timestamp, signer)];

  // If we're creating an older version of a request, we convert the string currency type to the new ICurrency one
  if (Semver.lt(action.data.version, '2.0.2')) {
    request.currency = legacyEnumToICurrencyConvert(action.data.parameters.currency);
  }

  const signerRole = Action.getRoleInAction(signer, action);
  if (signerRole === RequestLogicTypes.ROLE.PAYEE) {
    request.state = RequestLogicTypes.STATE.CREATED;
    request.creator = action.data.parameters.payee;
    return request;
  }
  if (signerRole === RequestLogicTypes.ROLE.PAYER) {
    request.state = RequestLogicTypes.STATE.ACCEPTED;
    request.creator = action.data.parameters.payer;
    return request;
  }

  throw new Error('Signer must be the payee or the payer');
}
github RequestNetwork / requestNetwork / packages / request-logic / src / actions / accept.ts View on Github external
function applyActionToRequest(
  action: RequestLogicTypes.IAction,
  timestamp: number,
  request: RequestLogicTypes.IRequest,
): RequestLogicTypes.IRequest {
  if (!action.data.parameters.requestId) {
    throw new Error('requestId must be given');
  }

  if (!request.payer) {
    throw new Error('the request must have a payer');
  }

  if (request.state !== RequestLogicTypes.STATE.CREATED) {
    throw new Error('the request state must be created');
  }

  const signer: IdentityTypes.IIdentity = Action.getSignerIdentityFromAction(action);
  const signerRole = Request.getRoleInRequest(signer, request);

  if (signerRole === RequestLogicTypes.ROLE.PAYER) {
    request.state = RequestLogicTypes.STATE.ACCEPTED;
  } else {
    throw new Error('Signer must be the payer');
  }
  // avoid to mutate the request
  let requestCopied: RequestLogicTypes.IRequest = Utils.deepCopy(request);
  requestCopied = Request.pushExtensionsData(requestCopied, action.data.parameters.extensionsData);
  requestCopied.events.push(generateEvent(action, timestamp, signer));
github RequestNetwork / requestNetwork / packages / request-logic / src / actions / cancel.ts View on Github external
request: RequestLogicTypes.IRequest,
): RequestLogicTypes.IRequest {
  if (!action.data.parameters.requestId) {
    throw new Error('requestId must be given');
  }

  const signer: IdentityTypes.IIdentity = Action.getSignerIdentityFromAction(action);
  const signerRole = Request.getRoleInRequest(signer, request);

  // avoid to mutate the request
  let requestCopied: RequestLogicTypes.IRequest = Utils.deepCopy(request);
  requestCopied = Request.pushExtensionsData(requestCopied, action.data.parameters.extensionsData);
  requestCopied.events.push(generateEvent(action, timestamp, signer));

  if (signerRole === RequestLogicTypes.ROLE.PAYER) {
    if (request.state !== RequestLogicTypes.STATE.CREATED) {
      throw new Error('A payer cancel need to be done on a request with the state created');
    }
    requestCopied.state = RequestLogicTypes.STATE.CANCELED;
    return requestCopied;
  }

  if (signerRole === RequestLogicTypes.ROLE.PAYEE) {
    if (request.state === RequestLogicTypes.STATE.CANCELED) {
      throw new Error('Cannot cancel an already canceled request');
    }
    requestCopied.state = RequestLogicTypes.STATE.CANCELED;
    return requestCopied;
  }

  throw new Error('Signer must be the payer or the payee');
}
github RequestNetwork / requestNetwork / packages / request-logic / src / actions / reduceExpectedAmount.ts View on Github external
throw new Error('deltaAmount must be given');
  }
  if (!Utils.amount.isValid(action.data.parameters.deltaAmount)) {
    throw new Error('deltaAmount must be a string representing a positive integer');
  }

  const signer: IdentityTypes.IIdentity = Action.getSignerIdentityFromAction(action);
  const signerRole = Request.getRoleInRequest(signer, request);

  // avoid to mutate the request
  let requestCopied: RequestLogicTypes.IRequest = Utils.deepCopy(request);
  requestCopied = Request.pushExtensionsData(requestCopied, action.data.parameters.extensionsData);
  requestCopied.events.push(generateEvent(action, timestamp, signer));

  if (signerRole === RequestLogicTypes.ROLE.PAYEE) {
    if (request.state === RequestLogicTypes.STATE.CANCELED) {
      throw new Error('the request must not be canceled');
    }
    // reduce the expected amount and store it as string or throw if the result is not valid
    requestCopied.expectedAmount = Utils.amount.reduce(
      request.expectedAmount,
      action.data.parameters.deltaAmount,
    );

    return requestCopied;
  }

  throw new Error('signer must be the payee');
}