EverydayTech Platform - Developer Reference
Complete Source Code Documentation - All Applications
Loading...
Searching...
No Matches
show-products.js
Go to the documentation of this file.
1#!/usr/bin/env node
2const { Pool } = require('pg');
3
4const pool = new Pool({
5 host: 'rmm-psa-db-do-user-28531160-0.i.db.ondigitalocean.com',
6 port: 25060,
7 database: 'defaultdb',
8 user: 'doadmin',
9 password: 'AVNS_J8RJAmsEwsHFG52_-F2',
10 ssl: { rejectUnauthorized: false }
11});
12
13/**
14 *
15 */
16async function showProducts() {
17 const tenantId = '00000000-0000-0000-0000-000000000001';
18
19 const result = await pool.query(`
20 SELECT
21 p.product_id,
22 p.name,
23 p.description,
24 p.unit_price,
25 COUNT(cli.line_item_id) as active_contracts,
26 SUM(cli.unit_price) as total_contract_value
27 FROM products p
28 LEFT JOIN contract_line_items cli ON p.product_id = cli.product_id
29 LEFT JOIN contracts c ON cli.contract_id = c.contract_id AND c.status = 'active'
30 WHERE p.tenant_id = $1
31 GROUP BY p.product_id, p.name, p.description, p.unit_price
32 ORDER BY p.unit_price DESC, active_contracts DESC
33 `, [tenantId]);
34
35 console.log('\n╔══════════════════════════════════════════════════════════╗');
36 console.log('║ Product Catalog with Corrected Pricing ║');
37 console.log('╚══════════════════════════════════════════════════════════╝\n');
38
39 let totalProductsWithPricing = 0;
40 let totalProductsInUse = 0;
41
42 for (const product of result.rows) {
43 const price = parseFloat(product.unit_price || 0);
44 const contracts = parseInt(product.active_contracts || 0);
45
46 if (contracts > 0) {
47 console.log(`${product.name}`);
48 console.log(` Unit Price: $${price.toFixed(2)}/month`);
49 console.log(` Active Contracts: ${contracts}`);
50 console.log(` Total Contract Value: $${parseFloat(product.total_contract_value || 0).toFixed(2)}`);
51 console.log('');
52 totalProductsInUse++;
53 }
54
55 if (price > 0) totalProductsWithPricing++;
56 }
57
58 console.log(`Summary: ${totalProductsInUse} products in active use, ${totalProductsWithPricing} products with pricing\n`);
59
60 await pool.end();
61}
62
63showProducts().catch(console.error);