-- 1. Create Agency Table CREATE TABLE Agency ( agency_id INT AUTO_INCREMENT PRIMARY KEY, agency_name VARCHAR(100) NOT NULL, city VARCHAR(100) ); -- 2. Create Development Table CREATE TABLE Development ( dev_id INT AUTO_INCREMENT PRIMARY KEY, agency_id INT, dev_name VARCHAR(100) NOT NULL, location VARCHAR(100), year_opened INT, height_stories INT, FOREIGN KEY (agency_id) REFERENCES Agency(agency_id) ON DELETE CASCADE ); -- 3. Create Household Table CREATE TABLE Household ( household_id INT AUTO_INCREMENT PRIMARY KEY, head_member_id INT NULL -- Will be linked after member insertion ); -- 4. Create HousingUnit Table CREATE TABLE HousingUnit ( unit_id INT AUTO_INCREMENT PRIMARY KEY, dev_id INT, household_id INT NULL, unit_number VARCHAR(20) NOT NULL, num_bedrooms INT, num_bathrooms INT, has_kitchen ENUM('Y', 'N'), has_living_room ENUM('Y', 'N'), sq_footage INT, FOREIGN KEY (dev_id) REFERENCES Development(dev_id) ON DELETE CASCADE, FOREIGN KEY (household_id) REFERENCES Household(household_id) ON DELETE SET NULL ); -- 5. Create Members Table CREATE TABLE Members ( member_id INT AUTO_INCREMENT PRIMARY KEY, household_id INT, member_name VARCHAR(100) NOT NULL, dob DATE, sex ENUM('M', 'F', 'O'), is_head ENUM('Y', 'N'), FOREIGN KEY (household_id) REFERENCES Household(household_id) ON DELETE CASCADE ); -- 6. Create UnitTransferHistory Table CREATE TABLE UnitTransferHistory ( transfer_id INT AUTO_INCREMENT PRIMARY KEY, household_id INT, from_unit_id INT, to_unit_id INT, transfer_date DATE NOT NULL, FOREIGN KEY (household_id) REFERENCES Household(household_id) ON DELETE CASCADE, FOREIGN KEY (from_unit_id) REFERENCES HousingUnit(unit_id) ON DELETE CASCADE, FOREIGN KEY (to_unit_id) REFERENCES HousingUnit(unit_id) ON DELETE CASCADE ); -- 7. Insert Dummy Seed Data INSERT INTO Agency (agency_name, city) VALUES ('Kolkata Housing Board', 'Kolkata'); INSERT INTO Development (agency_id, dev_name, location, year_opened, height_stories) VALUES (1, 'Greenwood Acres', 'North District', 2010, 5), (1, 'Riverside Towers', 'South Waterfront', 2018, 12); INSERT INTO Household (head_member_id) VALUES (NULL), (NULL); INSERT INTO HousingUnit (dev_id, household_id, unit_number, num_bedrooms, num_bathrooms, has_kitchen, has_living_room, sq_footage) VALUES (1, 1, 'A-101', 2, 1, 'Y', 'Y', 750), (1, NULL, 'B-202', 3, 2, 'Y', 'Y', 1100), (2, 2, 'T1-404', 1, 1, 'Y', 'N', 500), (2, NULL, 'T2-501', 2, 2, 'Y', 'Y', 900); INSERT INTO Members (household_id, member_name, dob, sex, is_head) VALUES (1, 'Amit Mukherjee', '1984-04-12', 'M', 'Y'), (2, 'Priya Sharma', '1991-08-23', 'F', 'Y'); -- Complete the circular reference loop for Heads UPDATE Household SET head_member_id = 1 WHERE household_id = 1; UPDATE Household SET head_member_id = 2 WHERE household_id = 2; const express = require('express'); const mysql = require('mysql2/promise'); const cors = require('cors'); const app = express(); app.use(cors()); app.use(express.json()); // XAMPP Default MySQL Connection Config const dbConfig = { host: 'localhost', user: 'root', password: '', database: 'housing_agency' }; // 1. Dropdown Lookup: Get all developments app.get('/api/developments', async (req, res) => { try { const connection = await mysql.createConnection(dbConfig); const [rows] = await connection.execute('SELECT dev_id, dev_name FROM Development ORDER BY dev_name'); await connection.end(); res.json(rows); } catch (err) { res.status(500).send(err.message); } }); // 2. Dropdown Lookup: Get all housing units app.get('/api/units', async (req, res) => { try { const connection = await mysql.createConnection(dbConfig); const [rows] = await connection.execute('SELECT unit_id, dev_id, unit_number FROM HousingUnit'); await connection.end(); res.json(rows); } catch (err) { res.status(500).send(err.message); } }); // 3. Dropdown Lookup: Get all household head members app.get('/api/heads', async (req, res) => { try { const connection = await mysql.createConnection(dbConfig); const [rows] = await connection.execute("SELECT h.household_id, m.member_name FROM Household h JOIN Members m ON h.head_member_id = m.member_id WHERE m.is_head = 'Y' ORDER BY m.member_name"); await connection.end(); res.json(rows); } catch (err) { res.status(500).send(err.message); } }); // 4. POST Endpoint to log a fresh unit transfer tracking entry app.post('/api/transfer', async (req, res) => { const { household_id, from_unit_id, to_unit_id, transfer_date } = req.body; try { const connection = await mysql.createConnection(dbConfig); // Transaction to add history log and update current active unit assignment await connection.beginTransaction(); const insertLogSql = `INSERT INTO UnitTransferHistory (household_id, from_unit_id, to_unit_id, transfer_date) VALUES (?, ?, ?, ?)`; await connection.execute(insertLogSql, [household_id, from_unit_id, to_unit_id, transfer_date]); const clearOldUnitSql = `UPDATE HousingUnit SET household_id = NULL WHERE unit_id = ?`; await connection.execute(clearOldUnitSql, [from_unit_id]); const assignNewUnitSql = `UPDATE HousingUnit SET household_id = ? WHERE unit_id = ?`; await connection.execute(assignNewUnitSql, [household_id, to_unit_id]); await connection.commit(); await connection.end(); res.json({ success: true }); } catch (err) { res.status(500).send(err.message); } }); // 5. GET Endpoint to fetch a date-wise report of head of household movements app.get('/api/report', async (req, res) => { try { const connection = await mysql.createConnection(dbConfig); const sql = ` SELECT m.member_name, u1.unit_number AS from_unit, u2.unit_number AS to_unit, t.transfer_date FROM UnitTransferHistory t JOIN Household h ON t.household_id = h.household_id JOIN Members m ON h.head_member_id = m.member_id JOIN HousingUnit u1 ON t.from_unit_id = u1.unit_id JOIN HousingUnit u2 ON t.to_unit_id = u2.unit_id ORDER BY t.transfer_date ASC `; const [rows] = await connection.execute(sql); await connection.end(); res.json(rows); } catch (err) { res.status(500).send(err.message); } }); app.listen(3000, () => console.log('XAMPP Backend microservice running on port 3000')); Public Housing Management Portal (XAMPP)

Household Unit Transfer Form

Household Relocation & Transfer Report

Head of Household From Unit To Unit Transfer Date