monzolib
Version:
Fully Featured JS/Node Monzo Library
292 lines (291 loc) • 8.53 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const date_fns_1 = require("date-fns");
const Amount_1 = require("./Amount");
const Attachment_1 = require("./Attachment");
const Merchant_1 = require("./Merchant");
class Transaction {
constructor(tx) {
this.tx = tx;
}
get amount() {
const domestic = {
amount: this.tx.amount,
currency: this.tx.currency,
};
if (this.tx.currency !== this.tx.local_currency) {
const local = {
amount: this.tx.local_amount,
currency: this.tx.local_currency,
};
return new Amount_1.Amount({ domestic, local });
}
else {
return new Amount_1.Amount({ domestic });
}
}
get attachments() {
if (this.tx.attachments) {
return this.tx.attachments.map(att => new Attachment_1.Attachment(att));
}
else {
return [];
}
}
get balance() {
const domestic = {
amount: this.tx.account_balance,
currency: this.tx.currency,
};
return new Amount_1.Amount({ domestic });
}
get category() {
let raw = this.tx.category;
raw = raw.replace('mondo', 'monzo');
let formatted = raw;
formatted = formatted.replace('_', ' ');
return {
raw,
formatted,
toString: () => raw,
};
}
get counterparty() {
return this.tx.counterparty;
}
get created() {
return date_fns_1.parseISO(this.tx.created);
}
get declined() {
return 'decline_reason' in this.tx;
}
get declineReason() {
if (!this.declined)
return undefined;
return this.tx.decline_reason
.replace(/_/g, ' ')
.toLowerCase()
.replace('cvc', 'CVC');
}
get description() {
return this.tx.description;
}
get displayName() {
if (this.counterparty && 'name' in this.counterparty) {
return this.counterparty.name;
}
else if (this.merchant &&
typeof this.merchant !== 'string' &&
'name' in this.merchant) {
return this.merchant.name;
}
else {
return this.description;
}
}
get displaySub() {
if (this.is.pot_withdraw) {
return 'Withdrawn from pot';
}
else if (this.is.pot_deposit) {
return 'Added to pot';
}
else if (this.declined) {
return this.declineReason;
}
else {
return this.notes.short.trim();
}
}
get hidden() {
return 'monux_hidden' in this.tx.metadata
? this.tx.metadata.monux_hidden === 'true'
: false;
}
get icon() {
if ('is_topup' in this.tx.metadata && this.tx.metadata.is_topup) {
return './assets/icons/topup.png';
}
else if (typeof this.merchant !== 'string' &&
this.merchant &&
'logo' in this.merchant &&
this.merchant.logo) {
return this.merchant.logo;
}
else if (this.tx.counterparty && 'user_id' in this.tx.counterparty) {
return './assets/icons/peer.png';
}
else
return this.iconFallback;
}
get iconFallback() {
return `./assets/icons/${this.category}.png`;
}
get id() {
return this.tx.id;
}
get is() {
const cash = String(this.category) === 'cash';
const zero = this.tx.amount === 0;
const metaAction = zero && !this.inSpending;
const pot = this.tx.scheme === 'uk_retail_pot';
const pot_deposit = 'pot_deposit_id' in this.tx.metadata;
const pot_withdraw = 'pot_withdrawal_id' in this.tx.metadata;
const auto_coin_jar = this.tx.metadata && this.tx.metadata.trigger === 'coin_jar';
const rounded = this.tx.metadata && 'coin_jar_transaction' in this.tx.metadata;
return {
metaAction,
cash,
zero,
pot,
pot_deposit,
pot_withdraw,
auto_coin_jar,
rounded,
};
}
get inSpending() {
return this.tx.include_in_spending || false;
}
get location() {
if ('merchant' in this.tx &&
typeof this.tx.merchant !== 'string' &&
this.tx.merchant &&
'online' in this.tx.merchant &&
this.tx.merchant.online) {
return 'Online';
}
else if ('merchant' in this.tx &&
typeof this.tx.merchant !== 'string' &&
this.tx.merchant &&
'address' in this.tx.merchant &&
this.tx.merchant.address &&
'short_formatted' in this.tx.merchant.address &&
this.tx.merchant.address.short_formatted) {
return this.tx.merchant.address.short_formatted;
}
else {
return '';
}
}
get merchant() {
if (!this.tx.merchant)
return undefined;
else if (typeof this.tx.merchant === 'string')
return this.tx.merchant;
else
return new Merchant_1.Merchant(this.tx.merchant);
}
get metadata() {
return this.tx.metadata;
}
get notes() {
const notes = this.tx.notes.replace('%2B', '+');
return {
short: notes.split('\n')[0],
full: notes,
toString: () => notes,
};
}
get online() {
return (!!this.tx.merchant &&
typeof this.tx.merchant !== 'string' &&
'online' in this.tx.merchant &&
this.tx.merchant.online);
}
get pending() {
if (this.declined)
return false;
if (this.is.cash)
return false;
if (this.tx.amount >= 0)
return false;
if (!('settled' in this.tx))
return true;
if ('settled' in this.tx && !this.tx.settled.trim())
return true;
return false;
}
get settled() {
if (this.pending)
return 'Pending';
else {
return `Settled: ${date_fns_1.format(date_fns_1.parseISO(this.tx.settled), 'h:mma - do MMMM yyyy')}`;
}
}
selfRequest() {
return transactionRequest(this.id);
}
changeCategoryRequest(category) {
return {
path: `/transactions/${this.id}`,
body: {
category: category,
},
method: 'PATCH',
};
}
annotateRequest(key, val) {
const metaKey = `metadata[${key}]`;
return {
path: `/transactions/${this.id}`,
body: {
[metaKey]: typeof val === 'string' ? val.replace('+', '%2B') : val,
},
method: 'PATCH',
};
}
setNotesRequest(val) {
return this.annotateRequest('notes', val);
}
hideRequest() {
return this.annotateRequest('monux_hidden', 'true');
}
unhideRequest() {
return this.annotateRequest('notes', '');
}
attachmentUploadRequest(contentType = 'image/jpeg') {
const random = Math.ceil(Math.random() * 9999);
const extensions = {
'image/jpeg': 'jpg',
'image/png': 'png',
'image/gif': 'gif',
};
const extension = contentType in extensions ? extensions[contentType] : 'image/jpeg';
return {
path: '/attachment/upload',
body: {
file_name: `monux-attachment-${random}.${extension}`,
file_type: contentType,
},
method: 'POST',
};
}
attachmentRegisterRequest(fileUrl, contentType = 'image/jpeg') {
return {
path: '/attachment/register',
body: {
external_id: this.tx.id,
file_url: fileUrl,
file_type: contentType,
},
method: 'POST',
};
}
get json() {
return this.tx;
}
get stringify() {
return JSON.stringify(this.json);
}
}
exports.Transaction = Transaction;
function transactionRequest(id) {
return {
path: `/transactions/${id}`,
qs: {
'expand[]': 'merchant',
},
};
}
exports.transactionRequest = transactionRequest;