2021-12-29 23:08:41 +00:00
|
|
|
import request from 'request-promise';
|
|
|
|
import { Logger } from '../../utils/logger.js';
|
|
|
|
import { Common } from '../../utils/common.js';
|
|
|
|
import { Database } from '../../utils/database.js';
|
|
|
|
import { CollectionFieldsEnum, CollectionsEnum } from '../../models/database.model.js';
|
|
|
|
let options = null;
|
|
|
|
const logger = Logger;
|
|
|
|
const common = Common;
|
|
|
|
const databaseService = Database;
|
|
|
|
export const listOfferBookmarks = (req, res, next) => {
|
|
|
|
logger.log({ selectedNode: req.session.selectedNode, level: 'INFO', fileName: 'Offers', msg: 'Getting Offer Bookmarks..' });
|
|
|
|
databaseService.find(req.session.selectedNode, CollectionsEnum.OFFERS).then((offers) => {
|
2022-01-16 20:55:50 +00:00
|
|
|
logger.log({ selectedNode: req.session.selectedNode, level: 'INFO', fileName: 'Offers', msg: 'Offer Bookmarks Received', data: offers });
|
2022-10-28 21:20:34 +00:00
|
|
|
res.status(200).json(offers);
|
2021-12-29 23:08:41 +00:00
|
|
|
}).catch((errRes) => {
|
|
|
|
const err = common.handleError(errRes, 'Offers', 'Offer Bookmarks Error', req.session.selectedNode);
|
|
|
|
return res.status(err.statusCode).json({ message: err.message, error: err.error });
|
|
|
|
});
|
|
|
|
};
|
|
|
|
export const deleteOfferBookmark = (req, res, next) => {
|
|
|
|
logger.log({ selectedNode: req.session.selectedNode, level: 'INFO', fileName: 'Offers', msg: 'Deleting Offer Bookmark..' });
|
2022-10-12 21:38:21 +00:00
|
|
|
databaseService.remove(req.session.selectedNode, CollectionsEnum.OFFERS, CollectionFieldsEnum.BOLT12, req.params.offerStr).then((deleteRes) => {
|
2022-01-16 20:55:50 +00:00
|
|
|
logger.log({ selectedNode: req.session.selectedNode, level: 'INFO', fileName: 'Offers', msg: 'Offer Bookmark Deleted', data: deleteRes });
|
2021-12-29 23:08:41 +00:00
|
|
|
res.status(204).json(req.params.offerStr);
|
|
|
|
}).catch((errRes) => {
|
|
|
|
const err = common.handleError(errRes, 'Offers', 'Offer Bookmark Delete Error', req.session.selectedNode);
|
|
|
|
return res.status(err.statusCode).json({ message: err.message, error: err.error });
|
|
|
|
});
|
|
|
|
};
|
|
|
|
export const listOffers = (req, res, next) => {
|
|
|
|
logger.log({ selectedNode: req.session.selectedNode, level: 'INFO', fileName: 'Offers', msg: 'Getting Offers..' });
|
|
|
|
options = common.getOptions(req);
|
|
|
|
if (options.error) {
|
|
|
|
return res.status(options.statusCode).json({ message: options.message, error: options.error });
|
|
|
|
}
|
|
|
|
options.url = req.session.selectedNode.ln_server_url + '/v1/offers/listoffers';
|
|
|
|
if (req.query.offer_id) {
|
|
|
|
options.url = options.url + '?offer_id=' + req.query.offer_id;
|
|
|
|
}
|
|
|
|
if (req.query.active_only) {
|
|
|
|
options.url = options.url + '?active_only=' + req.query.active_only;
|
|
|
|
}
|
|
|
|
logger.log({ selectedNode: req.session.selectedNode, level: 'DEBUG', fileName: 'Offers', msg: 'Offers List URL', data: options.url });
|
|
|
|
request(options).then((body) => {
|
2022-01-16 20:55:50 +00:00
|
|
|
logger.log({ selectedNode: req.session.selectedNode, level: 'INFO', fileName: 'Offers', msg: 'Offers List Received', data: body });
|
2021-12-29 23:08:41 +00:00
|
|
|
res.status(200).json(body);
|
|
|
|
}).catch((errRes) => {
|
|
|
|
const err = common.handleError(errRes, 'Offers', 'List Offers Error', req.session.selectedNode);
|
|
|
|
return res.status(err.statusCode).json({ message: err.message, error: err.error });
|
|
|
|
});
|
|
|
|
};
|
|
|
|
export const createOffer = (req, res, next) => {
|
|
|
|
logger.log({ selectedNode: req.session.selectedNode, level: 'INFO', fileName: 'Offers', msg: 'Creating Offer..' });
|
|
|
|
options = common.getOptions(req);
|
|
|
|
if (options.error) {
|
|
|
|
return res.status(options.statusCode).json({ message: options.message, error: options.error });
|
|
|
|
}
|
|
|
|
options.url = req.session.selectedNode.ln_server_url + '/v1/offers/offer';
|
|
|
|
options.body = req.body;
|
|
|
|
request.post(options).then((body) => {
|
2022-01-16 20:55:50 +00:00
|
|
|
logger.log({ selectedNode: req.session.selectedNode, level: 'INFO', fileName: 'Offers', msg: 'Offer Created', data: body });
|
2021-12-29 23:08:41 +00:00
|
|
|
res.status(201).json(body);
|
|
|
|
}).catch((errRes) => {
|
|
|
|
const err = common.handleError(errRes, 'Offer', 'Create Offer Error', req.session.selectedNode);
|
|
|
|
return res.status(err.statusCode).json({ message: err.message, error: err.error });
|
|
|
|
});
|
|
|
|
};
|
|
|
|
export const fetchOfferInvoice = (req, res, next) => {
|
|
|
|
logger.log({ selectedNode: req.session.selectedNode, level: 'INFO', fileName: 'Offers', msg: 'Getting Offer Invoice..' });
|
|
|
|
options = common.getOptions(req);
|
|
|
|
if (options.error) {
|
|
|
|
return res.status(options.statusCode).json({ message: options.message, error: options.error });
|
|
|
|
}
|
|
|
|
options.url = req.session.selectedNode.ln_server_url + '/v1/offers/fetchInvoice';
|
|
|
|
options.body = req.body;
|
|
|
|
logger.log({ selectedNode: req.session.selectedNode, level: 'DEBUG', fileName: 'Offers', msg: 'Offer Invoice Body', data: options.body });
|
|
|
|
request.post(options).then((body) => {
|
2022-01-16 20:55:50 +00:00
|
|
|
logger.log({ selectedNode: req.session.selectedNode, level: 'INFO', fileName: 'Offers', msg: 'Offer Invoice Received', data: body });
|
2021-12-29 23:08:41 +00:00
|
|
|
res.status(201).json(body);
|
|
|
|
}).catch((errRes) => {
|
|
|
|
const err = common.handleError(errRes, 'Offers', 'Get Offer Invoice Error', req.session.selectedNode);
|
|
|
|
return res.status(err.statusCode).json({ message: err.message, error: err.error });
|
|
|
|
});
|
|
|
|
};
|
|
|
|
export const disableOffer = (req, res, next) => {
|
|
|
|
logger.log({ selectedNode: req.session.selectedNode, level: 'INFO', fileName: 'Offers', msg: 'Disabling Offer..' });
|
|
|
|
options = common.getOptions(req);
|
|
|
|
if (options.error) {
|
|
|
|
return res.status(options.statusCode).json({ message: options.message, error: options.error });
|
|
|
|
}
|
|
|
|
options.url = req.session.selectedNode.ln_server_url + '/v1/offers/disableOffer/' + req.params.offerID;
|
|
|
|
request.delete(options).then((body) => {
|
2022-01-16 20:55:50 +00:00
|
|
|
logger.log({ selectedNode: req.session.selectedNode, level: 'INFO', fileName: 'Offers', msg: 'Offer Disabled', data: body });
|
2021-12-29 23:08:41 +00:00
|
|
|
res.status(202).json(body);
|
|
|
|
}).catch((errRes) => {
|
|
|
|
const err = common.handleError(errRes, 'Offers', 'Disable Offer Error', req.session.selectedNode);
|
|
|
|
return res.status(err.statusCode).json({ message: err.message, error: err.error });
|
|
|
|
});
|
|
|
|
};
|