src/models/agreement/Agreement.js
import AgreementSignatureModel from './AgreementSignature';
import AgreementTemplateModel from './AgreementTemplate';
/**
* Model representing an Agreement.
*/
class AgreementModel {
/**
* @param {Object} response - JSON formatted response of an agreement
* @param {Number} response.id - The id of the agreement
* @param {String} response.common_id - The common id of the agreement
* @param {String} response.status - The agreement status
* @param {Object} response.completed_at - The date when the agreement was created
* @param {Number} response.updated_at - The date when the agreement was last updated
* @param {String} response.document_url - The document url of the Agreement
* @param {Object} response.template - The agreement template object (V2)
* @param {Number} response.template.id - The agreement template object's id (V2)
* @param {String} response.template.redirect_url - The agreement template object's redirect url (V2)
* @param {Array} response.signatures - The list of signatures (V2)
*/
constructor(response = {}) {
const _response = response.data || response; // if json api is passed in directly
const attributes = _response.attributes || _response; // if json api is passed in directly
if (_response.id) {
/** @type {Number} */
this.id = parseInt(_response.id, 10);
}
/** @type {String} */
this.commonId = attributes.common_id;
/** @type {String} */
this.status = attributes.status;
/** @type {String} */
this.completedAt = attributes.completed_at;
/** @type {String} */
this.createdAt = attributes.created_at;
/** @type {String} */
this.updatedAt = attributes.updated_at;
/** @type {String} */
this.documentUrl = attributes.document_url;
// Old V2 attributes we need to support...
/** @type {Number} */
if (attributes.agreement_template_id) {
this.templateId = attributes.agreement_template_id;
}
/**
* @type {AgreementTemplate}
*/
if (attributes.template) {
this.template = new AgreementTemplateModel(attributes.template);
}
/** @type {AgreementSignatureModel[]} */
// this.signatures = response.signatures.map(obj => new AgreementSignatureModel(obj));
if (attributes.signatures) {
this.signatures = attributes.signatures.map(obj => new AgreementSignatureModel(obj));
}
}
}
export default AgreementModel;