|
1 | 1 | const express = require('express'); |
2 | 2 | const cors = require('cors'); |
| 3 | +const AvailabilityService = require('./services/availabilityService'); |
| 4 | + |
3 | 5 | const app = express(); |
4 | 6 | const port = 3000; |
5 | 7 |
|
6 | 8 | app.use(cors()); |
7 | 9 | app.use(express.json()); |
8 | 10 |
|
9 | 11 | app.get('/', (req, res) => { |
10 | | - res.json({ |
11 | | - project: 'LeaseFlow Protocol', |
| 12 | + res.json({ |
| 13 | + project: 'LeaseFlow Protocol', |
12 | 14 | status: 'Active', |
13 | 15 | contract_id: 'CAEGD57WVTVQSYWYB23AISBW334QO7WNA5XQ56S45GH6BP3D2AVHKUG4' |
14 | 16 | }); |
15 | 17 | }); |
16 | 18 |
|
| 19 | +app.get('/api/asset/:id/availability', async (req, res) => { |
| 20 | + try { |
| 21 | + const { id } = req.params; |
| 22 | + |
| 23 | + if (!id || isNaN(id)) { |
| 24 | + return res.status(400).json({ |
| 25 | + error: 'Invalid asset ID. Must be a number.', |
| 26 | + code: 'INVALID_ASSET_ID' |
| 27 | + }); |
| 28 | + } |
| 29 | + |
| 30 | + const availability = await availabilityService.getAssetAvailability(id); |
| 31 | + |
| 32 | + res.json({ |
| 33 | + success: true, |
| 34 | + data: availability |
| 35 | + }); |
| 36 | + |
| 37 | + } catch (error) { |
| 38 | + console.error(`Error fetching availability for asset ${req.params.id}:`, error); |
| 39 | + |
| 40 | + res.status(500).json({ |
| 41 | + error: 'Failed to fetch asset availability', |
| 42 | + code: 'FETCH_ERROR', |
| 43 | + details: process.env.NODE_ENV === 'development' ? error.message : undefined |
| 44 | + }); |
| 45 | + } |
| 46 | +}); |
| 47 | + |
| 48 | +app.get('/api/assets/availability', async (req, res) => { |
| 49 | + try { |
| 50 | + const { ids } = req.query; |
| 51 | + |
| 52 | + if (ids) { |
| 53 | + const assetIds = ids.split(',').map(id => id.trim()).filter(id => id && !isNaN(id)); |
| 54 | + |
| 55 | + if (assetIds.length === 0) { |
| 56 | + return res.status(400).json({ |
| 57 | + error: 'No valid asset IDs provided', |
| 58 | + code: 'INVALID_ASSET_IDS' |
| 59 | + }); |
| 60 | + } |
| 61 | + |
| 62 | + const availability = await availabilityService.getMultipleAssetAvailability(assetIds); |
| 63 | + |
| 64 | + res.json({ |
| 65 | + success: true, |
| 66 | + data: availability |
| 67 | + }); |
| 68 | + } else { |
| 69 | + const availability = await availabilityService.getAllAssetsAvailability(); |
| 70 | + |
| 71 | + res.json({ |
| 72 | + success: true, |
| 73 | + data: availability |
| 74 | + }); |
| 75 | + } |
| 76 | + |
| 77 | + } catch (error) { |
| 78 | + console.error('Error fetching assets availability:', error); |
| 79 | + |
| 80 | + res.status(500).json({ |
| 81 | + error: 'Failed to fetch assets availability', |
| 82 | + code: 'FETCH_ERROR', |
| 83 | + details: process.env.NODE_ENV === 'development' ? error.message : undefined |
| 84 | + }); |
| 85 | + } |
| 86 | +}); |
| 87 | + |
17 | 88 | if (require.main === module) { |
18 | | - app.listen(port, () => { |
19 | | - console.log(`LeaseFlow Backend listening at http://localhost:${port}`); |
| 89 | + const availabilityService = new AvailabilityService(); |
| 90 | + |
| 91 | + availabilityService.initialize().then(() => { |
| 92 | + app.locals.availabilityService = availabilityService; |
| 93 | + app.listen(port, () => { |
| 94 | + console.log(`LeaseFlow Backend listening at http://localhost:${port}`); |
| 95 | + console.log('Availability Service started'); |
| 96 | + }); |
| 97 | + }).catch(error => { |
| 98 | + console.error('Failed to initialize Availability Service:', error); |
| 99 | + process.exit(1); |
20 | 100 | }); |
21 | 101 | } |
22 | 102 |
|
| 103 | +const availabilityService = new AvailabilityService(); |
23 | 104 | module.exports = app; |
0 commit comments