/
websites
/
operateit-node
/
app
/
Controllers
/
Http
/
Upload File
HOME
'use strict' const ZohoSubscriptionService = use('App/Services/ZohoSubscriptionService') var fs = require('fs'); var Drive = use('Drive'); const Logger = use('Logger'); const axios = require('axios'); const Helpers = use('Helpers'); const path = require('path'); class ZohoSubscriptionController { zohoService = null; constructor() { this.zohoService = new ZohoSubscriptionService(); } async index ({ request, response }) { return await this.zohoService.getCustomerDetail('1910410000000103003'); } async customerDetail({params,response}){ let $response = { 'code' : 200, 'message' : "", 'customer' : null, }; if (params.id != "null"){ let $customer = await this.zohoService.getCustomerDetail(params.id); if ($customer) $response.customer = $customer; else{ $response.message = "Customer Not Found!"; $response.code = 500; } }else { $response.message = "Server Error!"; $response.code = 500; } response.status($response.code).send($response); } async subscriptionDetail({params,response}){ let $response = { 'code' : 200, 'message' : "", 'subscription' : null, }; if (params.customer_id != "null"){ let $subscription = await this.zohoService.getSubscriptionDetail(params.customer_id); if ($subscription) $response.subscription = $subscription; else{ $response.message = "Subscription Not Found!"; $response.code = 500; } }else { $response.message = "Server Error!"; $response.code = 500; } response.status($response.code).send($response); } async getInvoices({params,response}){ let $response = { 'code' : 200, 'message' : "", 'invoices' : null, }; if (params.customer_id && params.customer_id != "null"){ let $invoices = await this.zohoService.getCustomerInvoices(params.customer_id); if ($invoices) $response.invoices = $invoices; else{ $response.message = "Invoices Not Found!"; $response.code = 500; } }else { $response.message = "Server Error!"; $response.code = 500; } response.status($response.code).send($response); } async getSubscriptionInvoices({params,response}){ let $response = { 'code' : 200, 'message' : "", 'invoices' : null, }; if (params.subscription_id && params.subscription_id != "null"){ let $invoices = await this.zohoService.getSubscriptionInvoices(params.subscription_id); if ($invoices) $response.invoices = $invoices; else{ $response.message = "Invoices Not Found!"; $response.code = 500; } }else { $response.message = "Server Error!"; $response.code = 500; } response.status($response.code).send($response); } async find_user_email({ request, response }){ let $response = { 'code' : 200, 'message' : "", 'customer' : null, }; let $customer = await this.zohoService.findCustomerEmail(request.get().email); if ($customer) { $response.customer = $customer; } else{ $response.message = "Customer Not Found!"; $response.code = 500; } response.status($response.code).send($response); } async invoiceDetail({params,response}){ let $response = { 'code' : 200, 'message' : "", 'invoice' : null, }; if (params.id && params.id != "null"){ let $invoice = await this.zohoService.getInvoice(params.id); if ($invoice) $response.invoice = $invoice; else{ $response.message = "Invoices Not Found!"; $response.code = 500; } }else { $response.message = "Server Error!"; $response.code = 500; } response.status($response.code).send($response); } invoicePDF({params,response}){ response.implicitEnd = false; let $this = this; // let $invoice = await this.zohoService.getInvoicePDF(params.id); if (params.id) { let $file_name = params.file_name; if (!$file_name) $file_name = "InvoicePDF"; let $file_path = 'files/'+$file_name+'.pdf'; //Delete existing files const directory = 'tmp/files'; fs.readdir(directory, (err, files) => { if (err) throw err; for (const file of files) { fs.unlink(path.join(directory, file), err => { if (err) throw err; }); } }); //End Delete let $access_token = this.zohoService.getAccessToken().then(function(res){ let $header = { "cache-control": "no-cache", 'Authorization': 'Zoho-oauthtoken '+res, 'Accept': 'application/pdf', }; let $request_url = $this.zohoService.$BASE_URI+'invoices/'+params.id+"?organization_id="+$this.zohoService.$ORGANIZATION_ID; axios({ method:'get', url: $request_url, responseType: 'stream', headers: $header }) .then(function (res2) { var stream = res2.data.pipe(fs.createWriteStream("tmp/"+$file_path)); stream.on('finish', function () { response.attachment( Helpers.tmpPath($file_path) ) }); }).catch(function (error) { }); }); } } async getProductPlans({params,response}){ let $response = { 'code' : 200, 'message' : "", 'plans' : null, }; if (params.product_id && params.product_id != "null"){ let $plans = await this.zohoService.getProductPlans(params.product_id); if ($plans) $response.plans = $plans; else{ $response.message = "Plans Not Found!"; $response.code = 500; } }else { $response.message = "Server Error!"; $response.code = 500; } response.status($response.code).send($response); } async getPlanRecurringAddons({params,response}){ let $response = { 'code' : 200, 'message' : "", 'addons' : null, }; if (params.plan_code && params.plan_code != "null"){ let $addons = await this.zohoService.getPlanRecurringAddons(params.plan_code); if ($addons) $response.addons = $addons; else{ $response.message = "Addons Not Found!"; $response.code = 500; } }else { $response.message = "Server Error!"; $response.code = 500; } response.status($response.code).send($response); } async updateSubscription({params,request,response}){ let $response = { 'code' : 200, 'message' : "", 'subscription' : null, }; if (params.subscription_id && params.subscription_id != "null"){ let $data = request.post(); let $subscription = await this.zohoService.updateSubscription(params.subscription_id,$data); if ($subscription) $response.subscription = $subscription; else{ $response.message = "Subscription Update Error"; $response.code = 500; } }else { $response.message = "Server Error!"; $response.code = 500; } response.status($response.code).send($response); } async subscriptionMethodHostedPage({params,request,response}){ let $response = { 'code' : 200, 'message' : "", 'hostedpage' : null, }; if (params.subscription_id && params.subscription_id != "null"){ let $subscription = await this.zohoService.getSubscriptionHostedUrl(params.subscription_id); if ($subscription) $response.hostedpage = $subscription; else{ $response.message = "Hosted Url not found"; $response.code = 500; } }else { $response.message = "Server Error!"; $response.code = 500; } response.status($response.code).send($response); } } module.exports = ZohoSubscriptionController