How to use the @requestnetwork/types.RequestLogicTypes.ROLE 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 / create.ts View on Github external
// convert expectedAmount to string to have a consistent numbering
  requestParameters.expectedAmount = requestParameters.expectedAmount.toString();
  const version = Version.currentVersion;

  const unsignedAction: RequestLogicTypes.IUnsignedAction = {
    name: RequestLogicTypes.ACTION_NAME.CREATE,
    parameters: requestParameters,
    version,
  };

  const signerRole: RequestLogicTypes.ROLE = Action.getRoleInUnsignedAction(
    signerIdentity,
    unsignedAction,
  );

  if (signerRole !== RequestLogicTypes.ROLE.PAYEE && signerRole !== RequestLogicTypes.ROLE.PAYER) {
    throw new Error('Signer must be the payee or the payer');
  }

  return Action.createAction(unsignedAction, signerIdentity, signatureProvider);
}
github RequestNetwork / requestNetwork / packages / request-logic / src / actions / reduceExpectedAmount.ts View on Github external
if (!action.data.parameters.deltaAmount) {
    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');
}
github RequestNetwork / requestNetwork / packages / request-logic / src / actions / increaseExpectedAmount.ts View on Github external
if (!action.data.parameters.deltaAmount) {
    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 / cancel.ts View on Github external
timestamp: number,
  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 / accept.ts View on Github external
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));

  return requestCopied;
}
github RequestNetwork / requestNetwork / packages / request-logic / src / actions / cancel.ts View on Github external
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 / create.ts View on Github external
const signer: IdentityTypes.IIdentity = Action.getSignerIdentityFromAction(action);

  // Copy to not modify the action itself
  const request: RequestLogicTypes.IRequest = Utils.deepCopy(action.data.parameters);
  request.extensions = {};
  request.requestId = Action.getRequestId(action);
  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 / role.ts View on Github external
function getRole(identity: IdentityTypes.IIdentity, parameters: any): RequestLogicTypes.ROLE {
  if (parameters.payee && Utils.identity.areEqual(parameters.payee, identity)) {
    return RequestLogicTypes.ROLE.PAYEE;
  }
  if (parameters.payer && Utils.identity.areEqual(parameters.payer, identity)) {
    return RequestLogicTypes.ROLE.PAYER;
  }

  return RequestLogicTypes.ROLE.THIRD_PARTY;
}