text: "main";let ObjectID;_e08‍.w("mongodb",[["ObjectID",["ObjectID"],function(v){ObjectID=v}]]);let db;_e08‍.w("../../lib/mongo",[["db",["db"],function(v){db=v}]]);let utils;_e08‍.w("../../lib/utils",[["default",["utils"],function(v){utils=v}]]);let parse;_e08‍.w("../../lib/parse",[["default",["parse"],function(v){parse=v}]]);let webhooks;_e08‍.w("../../lib/webhooks",[["default",["webhooks"],function(v){webhooks=v}]]);let OrdersService;_e08‍.w("./orders",[["default",["OrdersService"],function(v){OrdersService=v}]]);
  
  
  
  
  
  
  class OrdertTansactionsService {
  	constructor() {}
  
  	async addTransaction(order_id, data) {
  		if (!_e08‍.a("ObjectID",ObjectID).isValid(order_id)) {
  			return Promise.reject('Invalid identifier');
  		}
  		let orderObjectID = new (_e08‍.a("ObjectID",ObjectID))(order_id);
  		const transaction = this.getValidDocumentForInsert(data);
  
  		await _e08‍.a("db",db).collection('orders').updateOne(
  			{
  				_id: orderObjectID
  			},
  			{
  				$push: {
  					transactions: transaction
  				}
  			}
  		);
  
  		const order = await _e08‍.a("OrdersService",OrdersService).getSingleOrder(order_id);
  		await _e08‍.a("webhooks",webhooks).trigger({
  			event: _e08‍.a("webhooks",webhooks).events.TRANSACTION_CREATED,
  			payload: order
  		});
  		return order;
  	}
  
  	async updateTransaction(order_id, transaction_id, data) {
  		if (!_e08‍.a("ObjectID",ObjectID).isValid(order_id) || !_e08‍.a("ObjectID",ObjectID).isValid(transaction_id)) {
  			return Promise.reject('Invalid identifier');
  		}
  		let orderObjectID = new (_e08‍.a("ObjectID",ObjectID))(order_id);
  		let transactionObjectID = new (_e08‍.a("ObjectID",ObjectID))(transaction_id);
  		const transaction = this.getValidDocumentForUpdate(data);
  
  		await _e08‍.a("db",db).collection('orders').updateOne(
  			{
  				_id: orderObjectID,
  				'transactions.id': transactionObjectID
  			},
  			{
  				$set: transaction
  			}
  		);
  
  		const order = await _e08‍.a("OrdersService",OrdersService).getSingleOrder(order_id);
  		await _e08‍.a("webhooks",webhooks).trigger({
  			event: _e08‍.a("webhooks",webhooks).events.TRANSACTION_UPDATED,
  			payload: order
  		});
  		return order;
  	}
  
  	async deleteTransaction(order_id, transaction_id) {
  		if (!_e08‍.a("ObjectID",ObjectID).isValid(order_id) || !_e08‍.a("ObjectID",ObjectID).isValid(transaction_id)) {
  			return Promise.reject('Invalid identifier');
  		}
  		let orderObjectID = new (_e08‍.a("ObjectID",ObjectID))(order_id);
  		let transactionObjectID = new (_e08‍.a("ObjectID",ObjectID))(transaction_id);
  
  		await _e08‍.a("db",db).collection('orders').updateOne(
  			{
  				_id: orderObjectID
  			},
  			{
  				$pull: {
  					transactions: {
  						id: transactionObjectID
  					}
  				}
  			}
  		);
  
  		const order = await _e08‍.a("OrdersService",OrdersService).getSingleOrder(order_id);
  		await _e08‍.a("webhooks",webhooks).trigger({
  			event: _e08‍.a("webhooks",webhooks).events.TRANSACTION_DELETED,
  			payload: order
  		});
  		return order;
  	}
  
  	getValidDocumentForInsert(data) {
  		return {
  			id: new (_e08‍.a("ObjectID",ObjectID))(),
  			transaction_id: _e08‍.a("parse",parse).getString(data.transaction_id),
  			amount: _e08‍.a("parse",parse).getNumberIfPositive(data.amount) || 0,
  			currency: _e08‍.a("parse",parse).getString(data.currency),
  			status: _e08‍.a("parse",parse).getString(data.status),
  			details: _e08‍.a("parse",parse).getString(data.details),
  			success: _e08‍.a("parse",parse).getBooleanIfValid(data.success)
  		};
  	}
  
  	getValidDocumentForUpdate(data) {
  		if (Object.keys(data).length === 0) {
  			return new Error('Required fields are missing');
  		}
  
  		let transaction = {};
  
  		if (data.transaction_id !== undefined) {
  			transaction['transactions.$.transaction_id'] = _e08‍.a("parse",parse).getString(
  				data.transaction_id
  			);
  		}
  
  		if (data.amount !== undefined) {
  			transaction['transactions.$.amount'] =
  				_e08‍.a("parse",parse).getNumberIfPositive(data.amount) || 0;
  		}
  
  		if (data.currency !== undefined) {
  			transaction['transactions.$.currency'] = _e08‍.a("parse",parse).getString(data.currency);
  		}
  
  		if (data.status !== undefined) {
  			transaction['transactions.$.status'] = _e08‍.a("parse",parse).getString(data.status);
  		}
  
  		if (data.details !== undefined) {
  			transaction['transactions.$.details'] = _e08‍.a("parse",parse).getString(data.details);
  		}
  
  		if (data.success !== undefined) {
  			transaction['transactions.$.success'] = _e08‍.a("parse",parse).getBooleanIfValid(
  				data.success
  			);
  		}
  
  		return transaction;
  	}
  }
  
  _e08‍.d(new OrdertTansactionsService());_e08‍.u(["default"]);
  