| [7517a3a] | 1 | const db = require('../config/database');
|
|---|
| 2 |
|
|---|
| 3 | const adminQueries = {
|
|---|
| 4 | getBuildingsByAdminId: async (admin_id) => {
|
|---|
| 5 | const query = `
|
|---|
| 6 | SELECT building_id, name, address, description
|
|---|
| 7 | FROM building
|
|---|
| 8 | WHERE admin_id = $1
|
|---|
| 9 | ORDER BY name
|
|---|
| 10 | `;
|
|---|
| 11 | const result = await db.getPool().query(query, [admin_id]);
|
|---|
| 12 | return result.rows;
|
|---|
| 13 | },
|
|---|
| 14 |
|
|---|
| 15 | getFloorsByBuildingId: async (buildingId) => {
|
|---|
| 16 | const query = `
|
|---|
| 17 | SELECT floor_id, floor_number, layout_image
|
|---|
| 18 | FROM floor
|
|---|
| 19 | WHERE building_id = $1
|
|---|
| 20 | ORDER BY floor_number
|
|---|
| 21 | `;
|
|---|
| 22 | const result = await db.getPool().query(query, [buildingId]);
|
|---|
| 23 | return result.rows;
|
|---|
| 24 | },
|
|---|
| 25 |
|
|---|
| 26 | validateUnitNumber: async (floorId, unitNumber) => {
|
|---|
| 27 | const query = `
|
|---|
| 28 | SELECT COUNT(*) as count
|
|---|
| 29 | FROM unit
|
|---|
| 30 | WHERE floor_id = $1
|
|---|
| 31 | AND unit_number = $2
|
|---|
| 32 | `;
|
|---|
| 33 | const result = await db.getPool().query(query, [floorId, unitNumber]);
|
|---|
| 34 | return result.rows[0].count === '0';
|
|---|
| 35 | },
|
|---|
| 36 |
|
|---|
| 37 | insertUnit: async (unitData, floorId) => {
|
|---|
| 38 | const query = `
|
|---|
| 39 | INSERT INTO unit (unit_number, room_number, floor_area, status, price, image, floorplan, vector_image, floor_id)
|
|---|
| 40 | VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
|
|---|
| 41 | RETURNING unit_id, unit_number, room_number, floor_area, status, price
|
|---|
| 42 | `;
|
|---|
| 43 | const values = [
|
|---|
| 44 | unitData.unit_number,
|
|---|
| 45 | unitData.room_number,
|
|---|
| 46 | unitData.floor_area,
|
|---|
| 47 | unitData.status,
|
|---|
| 48 | unitData.price,
|
|---|
| 49 | unitData.image,
|
|---|
| 50 | unitData.floorplan,
|
|---|
| 51 | unitData.vector_image,
|
|---|
| 52 | floorId,
|
|---|
| 53 | ];
|
|---|
| 54 | const result = await db.getPool().query(query, values);
|
|---|
| 55 | return result.rows[0];
|
|---|
| 56 | },
|
|---|
| 57 |
|
|---|
| 58 | getBuildingById: async (buildingId) => {
|
|---|
| 59 | const query = `
|
|---|
| 60 | SELECT building_id, name, address, description, admin_id
|
|---|
| 61 | FROM building
|
|---|
| 62 | WHERE building_id = $1
|
|---|
| 63 | `;
|
|---|
| 64 | const result = await db.getPool().query(query, [buildingId]);
|
|---|
| 65 | return result.rows[0];
|
|---|
| 66 | },
|
|---|
| 67 |
|
|---|
| 68 | getFloorById: async (floorId) => {
|
|---|
| 69 | const query = `
|
|---|
| 70 | SELECT floor_id, floor_number, layout_image, building_id
|
|---|
| 71 | FROM floor
|
|---|
| 72 | WHERE floor_id = $1
|
|---|
| 73 | `;
|
|---|
| 74 | const result = await db.getPool().query(query, [floorId]);
|
|---|
| 75 | return result.rows[0];
|
|---|
| 76 | },
|
|---|
| 77 | };
|
|---|
| 78 |
|
|---|
| 79 | const clientQueries = {
|
|---|
| 80 | getAllBuildings: async () => {
|
|---|
| 81 | const query = `
|
|---|
| 82 | SELECT building_id, name, address, description
|
|---|
| 83 | FROM building
|
|---|
| 84 | ORDER BY name
|
|---|
| 85 | `;
|
|---|
| 86 | const result = await db.getPool().query(query);
|
|---|
| 87 | return result.rows;
|
|---|
| 88 | },
|
|---|
| 89 |
|
|---|
| 90 | getBuildingDetailById: async (buildingId) => {
|
|---|
| 91 | const query = `
|
|---|
| 92 | SELECT
|
|---|
| 93 | b.building_id,
|
|---|
| 94 | b.name AS building_name,
|
|---|
| 95 | b.address,
|
|---|
| 96 | b.description,
|
|---|
| 97 | a.full_name AS architect_name,
|
|---|
| 98 | f.floor_id,
|
|---|
| 99 | f.floor_number,
|
|---|
| 100 | f.layout_image
|
|---|
| 101 | FROM building b
|
|---|
| 102 | LEFT JOIN designs d ON b.building_id = d.building_id
|
|---|
| 103 | LEFT JOIN architect a ON d.architect_id = a.architect_id
|
|---|
| 104 | JOIN floor f ON b.building_id = f.building_id
|
|---|
| 105 | WHERE b.building_id = $1
|
|---|
| 106 | ORDER BY f.floor_number
|
|---|
| 107 | `;
|
|---|
| 108 | const result = await db.getPool().query(query, [buildingId]);
|
|---|
| 109 | return result.rows;
|
|---|
| 110 | },
|
|---|
| 111 |
|
|---|
| 112 | getUnitsByFloorId: async (floorId) => {
|
|---|
| 113 | const query = `
|
|---|
| 114 | SELECT
|
|---|
| 115 | u.unit_id,
|
|---|
| 116 | u.unit_number,
|
|---|
| 117 | u.room_number,
|
|---|
| 118 | u.floor_area,
|
|---|
| 119 | u.price,
|
|---|
| 120 | u.status,
|
|---|
| 121 | u.image
|
|---|
| 122 | FROM unit u
|
|---|
| 123 | WHERE u.floor_id = $1
|
|---|
| 124 | ORDER BY u.unit_number
|
|---|
| 125 | `;
|
|---|
| 126 | const result = await db.getPool().query(query, [floorId]);
|
|---|
| 127 | return result.rows;
|
|---|
| 128 | },
|
|---|
| 129 |
|
|---|
| 130 | getUnitById: async (unitId) => {
|
|---|
| 131 | const query = `
|
|---|
| 132 | SELECT
|
|---|
| 133 | u.unit_id,
|
|---|
| 134 | u.unit_number,
|
|---|
| 135 | u.room_number,
|
|---|
| 136 | u.floor_area,
|
|---|
| 137 | u.price,
|
|---|
| 138 | u.status,
|
|---|
| 139 | u.image,
|
|---|
| 140 | u.floorplan,
|
|---|
| 141 | u.vector_image,
|
|---|
| 142 | f.floor_id,
|
|---|
| 143 | f.floor_number,
|
|---|
| 144 | b.building_id,
|
|---|
| 145 | b.name AS building_name
|
|---|
| 146 | FROM unit u
|
|---|
| 147 | JOIN floor f ON u.floor_id = f.floor_id
|
|---|
| 148 | JOIN building b ON f.building_id = b.building_id
|
|---|
| 149 | WHERE u.unit_id = $1
|
|---|
| 150 | `;
|
|---|
| 151 | const result = await db.getPool().query(query, [unitId]);
|
|---|
| 152 | return result.rows[0];
|
|---|
| 153 | },
|
|---|
| 154 | };
|
|---|
| 155 |
|
|---|
| 156 | const appointmentQueries = {
|
|---|
| 157 | getAvailableTimeslots: async () => {
|
|---|
| 158 | const query = `
|
|---|
| 159 | SELECT
|
|---|
| 160 | a.agent_id,
|
|---|
| 161 | a.name AS agent_name,
|
|---|
| 162 | a.email,
|
|---|
| 163 | t.timeslot_id,
|
|---|
| 164 | t.date,
|
|---|
| 165 | t.time_start,
|
|---|
| 166 | t.time_end
|
|---|
| 167 | FROM agent a
|
|---|
| 168 | JOIN timeslot t ON a.agent_id = t.agent_id
|
|---|
| 169 | WHERE t.status = 'Available'
|
|---|
| 170 | AND t.date >= CURRENT_DATE
|
|---|
| 171 | ORDER BY a.name, t.date, t.time_start
|
|---|
| 172 | `;
|
|---|
| 173 | const result = await db.getPool().query(query);
|
|---|
| 174 | return result.rows;
|
|---|
| 175 | },
|
|---|
| 176 |
|
|---|
| 177 | // upsert: update client info on email conflict
|
|---|
| 178 | createClient: async (name, email, phone) => {
|
|---|
| 179 | const query = `
|
|---|
| 180 | INSERT INTO client (name, email, phone)
|
|---|
| 181 | VALUES ($1, $2, $3)
|
|---|
| 182 | ON CONFLICT (email) DO UPDATE SET name = $1, phone = $3
|
|---|
| 183 | RETURNING client_id
|
|---|
| 184 | `;
|
|---|
| 185 | const result = await db.getPool().query(query, [name, email, phone]);
|
|---|
| 186 | return result.rows[0].client_id;
|
|---|
| 187 | },
|
|---|
| 188 |
|
|---|
| 189 | verifyTimeslot: async (timeslotId) => {
|
|---|
| 190 | const query = `
|
|---|
| 191 | SELECT status
|
|---|
| 192 | FROM timeslot
|
|---|
| 193 | WHERE timeslot_id = $1
|
|---|
| 194 | `;
|
|---|
| 195 | const result = await db.getPool().query(query, [timeslotId]);
|
|---|
| 196 | return result.rows[0]?.status === 'Available';
|
|---|
| 197 | },
|
|---|
| 198 |
|
|---|
| 199 | getAgentFromTimeslot: async (timeslotId) => {
|
|---|
| 200 | const query = `
|
|---|
| 201 | SELECT agent_id
|
|---|
| 202 | FROM timeslot
|
|---|
| 203 | WHERE timeslot_id = $1
|
|---|
| 204 | `;
|
|---|
| 205 | const result = await db.getPool().query(query, [timeslotId]);
|
|---|
| 206 | return result.rows[0]?.agent_id;
|
|---|
| 207 | },
|
|---|
| 208 |
|
|---|
| 209 | createAppointment: async (clientId, unitId, timeslotId) => {
|
|---|
| 210 | const query = `
|
|---|
| 211 | INSERT INTO appointment (status, client_id, unit_id, timeslot_id)
|
|---|
| 212 | VALUES ('Scheduled', $1, $2, $3)
|
|---|
| 213 | RETURNING appointment_id
|
|---|
| 214 | `;
|
|---|
| 215 | const result = await db.getPool().query(query, [clientId, unitId, timeslotId]);
|
|---|
| 216 | return result.rows[0].appointment_id;
|
|---|
| 217 | },
|
|---|
| 218 |
|
|---|
| 219 | bookTimeslot: async (timeslotId) => {
|
|---|
| 220 | const query = `
|
|---|
| 221 | UPDATE timeslot
|
|---|
| 222 | SET status = 'Booked'
|
|---|
| 223 | WHERE timeslot_id = $1
|
|---|
| 224 | `;
|
|---|
| 225 | await db.getPool().query(query, [timeslotId]);
|
|---|
| 226 | },
|
|---|
| 227 |
|
|---|
| 228 | getAppointmentDetails: async (appointmentId) => {
|
|---|
| 229 | const query = `
|
|---|
| 230 | SELECT
|
|---|
| 231 | ap.appointment_id,
|
|---|
| 232 | c.name AS client_name,
|
|---|
| 233 | c.email AS client_email,
|
|---|
| 234 | c.phone AS client_phone,
|
|---|
| 235 | u.unit_number,
|
|---|
| 236 | b.name AS building_name,
|
|---|
| 237 | f.floor_number,
|
|---|
| 238 | t.date,
|
|---|
| 239 | t.time_start,
|
|---|
| 240 | t.time_end,
|
|---|
| 241 | a.name AS agent_name,
|
|---|
| 242 | a.email AS agent_email
|
|---|
| 243 | FROM appointment ap
|
|---|
| 244 | JOIN client c ON ap.client_id = c.client_id
|
|---|
| 245 | JOIN unit u ON ap.unit_id = u.unit_id
|
|---|
| 246 | JOIN floor f ON u.floor_id = f.floor_id
|
|---|
| 247 | JOIN building b ON f.building_id = b.building_id
|
|---|
| 248 | JOIN timeslot t ON ap.timeslot_id = t.timeslot_id
|
|---|
| 249 | JOIN agent a ON t.agent_id = a.agent_id
|
|---|
| 250 | WHERE ap.appointment_id = $1
|
|---|
| 251 | `;
|
|---|
| 252 | const result = await db.getPool().query(query, [appointmentId]);
|
|---|
| 253 | return result.rows[0];
|
|---|
| 254 | },
|
|---|
| 255 | };
|
|---|
| 256 |
|
|---|
| 257 | module.exports = {
|
|---|
| 258 | adminQueries,
|
|---|
| 259 | clientQueries,
|
|---|
| 260 | appointmentQueries,
|
|---|
| 261 | }; |
|---|