Skip to content

Commit

Permalink
Merge 4e71ff4 into e69a225
Browse files Browse the repository at this point in the history
  • Loading branch information
manuelvilche committed Dec 23, 2019
2 parents e69a225 + 4e71ff4 commit 28bee26
Show file tree
Hide file tree
Showing 10 changed files with 4,035 additions and 32 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Added
- New package to send emails
59 changes: 55 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,67 @@ A package to handle emails

## Installation
```sh
npm install @janiscommerce/mail/mail
npm install @janiscommerce/mail
```

## API
### Mail structure
The `Mail [Object]` parameter have the following structure:
- **`to [String|Array]`** (required): The emails addresses to send the email
- **`templateCode [String|Array]`** (required): The code of the template
- **`data [Object]`** (optional): This property is a JSON that includes all the data that can use in the template email.
- **`subject [String]`** (optional): This is the subject of the email string.
- **`cc [String|Array]`** (optional): The emails addresses to send the email in the cc field.
- **`bcc [String|Array]`** (optional): The emails addresses to send the email in the bcc field.
- **`replyTo [String|Array]`** (optional): The emails addresses to send the replyTo of the email.
- **`entity [String|Number]`** (optional): The entity to associated to the email.
- **`entity_id [String|Number]`** (optional): The entity Id to associated to the email.

### Mail example
```js
{
to: 'info@janis.im',
templateCode: 'template-code-name',
cc: 'log',
bcc: 'mail@example.com',
replyTo: ['mail@example.com'],
subject: 'email subject',
entity: 'order',
entityId: '0acefd5e-cb90-4531-b27a-e4d236f07539',
data: {
name: 'exampleName',
lastName: 'exampleLastName'
}
}
```

## Usage
```js
const Mail = require('@janiscommerce/mail/mail');
const Mail = require('@janiscommerce/mail');

Mail.setTo('some-client')
.setCC('mail@example.com')
.setBCC(['mail@example.com'])
.setReplyTo(['mail@example.com'])
.setSubject('Email Subject')
.setEntity('order')
.setEntityId('5de565c07de99000110dcdef')
.setData({
someField: 'someFieldValue',
otherField: 'otherFieldValue'
})
.setTemplateCode('template-code')
.send();
```

## Examples
## Errors

The errors are informed with a `MailError`.
This object has a code that can be useful for a correct error handling.
The codes are the following:

| Code | Description |
|------|--------------------------------|
| 1 | Requiered field missing |
| 2 | Invalid field type |
| 3 | No Service Name Setted |
| 4 | Microservice call Error |
5 changes: 0 additions & 5 deletions index.js

This file was deleted.

9 changes: 0 additions & 9 deletions lib/index.js

This file was deleted.

14 changes: 11 additions & 3 deletions lib/mail-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,24 @@ class MailError extends Error {
static get codes() {

return {
// your errors here...
REQUIRED_FIELD_MISSING: 1,
INVALID_FIELD_TYPE: 2,
MS_CALL_ERROR: 3
};

}

constructor(err, code) {
super(err);
this.message = err.message || err;

const message = err.message || err;

super(message);
this.message = message;
this.code = code;
this.name = 'MailError';

if(err instanceof Error)
this.previousError = err;
}
}

Expand Down
59 changes: 59 additions & 0 deletions lib/mail-validator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
'use strict';

const MailError = require('./mail-error');

class MailValidator {

constructor({ _mail }) {
this.mail = _mail;
}

validateRequiredFields() {

if(typeof this.mail.to === 'undefined')
throw new MailError('Empty field: to property is required', MailError.codes.REQUIRED_FIELD_MISSING);

if(typeof this.mail.templateCode === 'undefined')
throw new MailError('Empty field: templateCode property is required', MailError.codes.REQUIRED_FIELD_MISSING);
}

validateFieldTypes() {

if(typeof this.mail.templateCode !== 'string' && typeof this.mail.templateCode !== 'number')
throw new MailError('Invalid mail: templateCode property must be a string or number', MailError.codes.INVALID_FIELD_TYPE);

// Should be an array or string
if(!Array.isArray(this.mail.to) && typeof this.mail.to !== 'string')
throw new MailError('Invalid mail: to property must be an array or string', MailError.codes.INVALID_FIELD_TYPE);

// Should be an object (not array)
if(typeof this.mail.data !== 'undefined' && typeof this.mail.data !== 'object')
throw new MailError('Invalid mail: data property should be a object', MailError.codes.INVALID_FIELD_TYPE);

// Should be a string
if(typeof this.mail.subject !== 'undefined' && typeof this.mail.subject !== 'string' && typeof this.mail.subject !== 'number')
throw new MailError('Invalid mail: subject property should be a string or number', MailError.codes.INVALID_FIELD_TYPE);

// Should be an array or string
if(typeof this.mail.cc !== 'undefined' && (!Array.isArray(this.mail.cc) && typeof this.mail.cc !== 'string'))
throw new MailError('Invalid mail: cc property must be an array or string', MailError.codes.INVALID_FIELD_TYPE);

// Should be an array or string
if(typeof this.mail.bcc !== 'undefined' && (!Array.isArray(this.mail.bcc) && typeof this.mail.bcc !== 'string'))
throw new MailError('Invalid mail: bcc property must be an array or string', MailError.codes.INVALID_FIELD_TYPE);

// Should be an array or string
if(typeof this.mail.replyTo !== 'undefined' && (!Array.isArray(this.mail.replyTo) && typeof this.mail.replyTo !== 'string'))
throw new MailError('Invalid mail: replyTo property must be an array or string', MailError.codes.INVALID_FIELD_TYPE);

// Should be a string or number
if(typeof this.mail.entity !== 'undefined' && (typeof this.mail.entity !== 'string' && typeof this.mail.entity !== 'number'))
throw new MailError('Invalid mail: entity property must be an string or number', MailError.codes.INVALID_FIELD_TYPE);

// Should be a string or number
if(typeof this.mail.entityId !== 'undefined' && (typeof this.mail.entityId !== 'string' && typeof this.mail.entityId !== 'number'))
throw new MailError('Invalid mail: entityId property must be an string or number', MailError.codes.INVALID_FIELD_TYPE);
}
}

module.exports = MailValidator;
145 changes: 144 additions & 1 deletion lib/mail.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,153 @@
'use strict';

const MicroserviceCall = require('@janiscommerce/microservice-call');

const MailError = require('./mail-error');
const MailValidator = require('./mail-validator');

const JANIS_MAILING_SERVICE = 'mailing';
const JANIS_MAILING_NAMESPACE = 'email';
const JANIS_MAILING_METHOD = 'create';

class Mail {

// package code...
get _mailingService() {
return JANIS_MAILING_SERVICE;
}

get _mailingNamespace() {
return JANIS_MAILING_NAMESPACE;
}

get _mailingMethod() {
return JANIS_MAILING_METHOD;
}

constructor() {
this._mail = {};
return this;
}

setTo(to) {
this._mail.to = to;
return this;
}

/**
* Adds a cc emails.
*
* @param {string|array} cc CC emails
*/
setCC(cc) {
this._mail.cc = cc;
return this;
}

/**
* Adds a bcc emails.
*
* @param {string|array} bcc The bcc emails
*/
setBCC(bcc) {
this._mail.bcc = bcc;
return this;
}

/**
* Sets the reply to field.
*
* @param {<type>} replyTo The reply to emails
*/
setReplyTo(replyTo) {
this._mail.replyTo = replyTo;
return this;
}

/**
* Sets the template code.
*
* @param {string} templateCode The template code
*/
setTemplateCode(templateCode) {
this._mail.templateCode = templateCode;
return this;
}

/**
* Sets the subject.
*
* @param {string} subject The subject
*/
setSubject(subject) {
this._mail.subject = subject;
return this;
}

/**
* Sets the entity associate to the email.
*
* @param {string|number} entity The entity
*/
setEntity(entity) {
this._mail.entity = entity;
return this;
}

/**
* Sets the entity identifier.
*
* @param {string|number} entityId The entity identifier
*/
setEntityId(entityId) {
this._mail.entityId = entityId;
return this;
}

/**
* Sets the data.
*
* @param {object} data The data
*/
setData(data) {
this._mail.data = data;
return this;
}

/**
* Validate email data
*/
_validateMail() {

const mailValidator = new MailValidator(this);

mailValidator.validateRequiredFields();

mailValidator.validateFieldTypes();
}

/**
* Send the email
*
* @return {Object} The response of the MicroserviceCall
*/
async send() {

this._validateMail();

const ms = new MicroserviceCall();

try {

const response = await ms.post(this._mailingService, this._mailingNamespace, this._mailingMethod, this._mail);

return {
id: response.body.id
};

} catch(err) {
throw new MailError(err, MailError.codes.MS_CALL_ERROR);
}
}

}

Expand Down
Loading

0 comments on commit 28bee26

Please sign in to comment.