# CardX SQS

SQS allows jobs (messages) to be queued and consumed at a configurable rate.
This library helps create SQS messages with the specific schema required for a particular queue.

## Installation
``` bash
npm --save install @cardx/sqs
```

## SQS policy access
**IMPORTANT** Although these functions can help you send SQS messages to particular queues, your lambda resources still need to have access to the particular queue.
Example resource creation in js CDK:
```js
const lambda; // create your lambda
const QUEUE_ARN; // get this in a number of ways. perhaps importing it from another stack.
const queueAccess = new PolicyStatement({
  effect: Effect.ALLOW,
  resources: [QUEUE_ARN],
  actions: ["sqs:SendMessage", "sqs:SendMessageBatch", "sqs:GetQueueUrl"]
});
// add the policy to the queue. Now it is allowed to send messages!
lambda.addToRolePolicy(queueAccess);
```

## Usage

``` javascript
import SQS from "@cardx/sqs";

SQS.setLogger(log); // Optionally pass a @cardx/logger object
```

## Specific-Queue Functions

### Send Message Mail
Sends an SQS message to the Mail repo. Should result in an email being sent via SendGrid.
```js
await SQS.sendMessageMail ({
  template, // string name of the email template you would like to send.
  pnpAccount, // optional if an email sent to the merchant, required if an email sent to a merchant's customer.
  html_data, // object of key:value pairs that will be insertable into the html template,
  to, // csv string of 'to' addresses (ex. `x@x.com, x@y.com`). May have a template default.
  from, // string of 'from' address (ex. `no_reply@cardx.com`). May have a template default.
  bcc, // csv string of 'bcc' addresses (ex. `x@x.com, x@y.com`). May be set via the portal, passing a value here would override that.
  subject, // string subject line of email (ex. 'Transaction Receipt'),
  origin, // optional. mapper inside mail repo looks at this value in case the particular template has different key:value pairs from different sources
});
```

## Core Functions

### sendMessage
The specific-queue helper functions leverage this function to do the actual SQS message sending.
You can also use it directly if a helper function seems excessive.
```js
await SQS.sendMessage({
  body, // js object with key-value pairs you want to send in message
  queueUrl, // url of the SQS resource being sent to
  messageAttributes, // not required. object with key:value pair for each message 'attribute'
  isFifo, // boolean - if true (default), will generate the necessary fifo params
  delaySeconds, // delaySeconds - integer - not required. set up to 900 seconds to delay the message being consumed.
});
```

### getMessagesFromEvent
Returns an array of messages from an 'event' created by a SQS trigger.
Each message has a `body` key and a `attributes` key for the relevant data.
```js
const messages = await getMessagesFromEvent (lambdaEvent);
```

### getMessageBody **TODO**

## Changelog

* 0.3.0 - add 'getmessagesFromEvent' function
* 0.2.0 - add 'sendMessageTransactionAuth' function
* 0.1.6 - add 'delaySeconds' option to the 'sendMessage' function
* 0.1.5 - working version

## License

© CardX. All rights reserved.
