Skip to content

Commit

Permalink
Add more criteria support
Browse files Browse the repository at this point in the history
  • Loading branch information
qiaozhengyuan committed Sep 19, 2024
1 parent 98e511b commit 02d8140
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 7 deletions.
2 changes: 2 additions & 0 deletions sql/db_setup.sql
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ CREATE TABLE Applicants (
date_of_birth DATE NOT NULL,
marital_status marital_status_enum NOT NULL,
employment_status employment_status_enum NOT NULL,
date_unemployed DATE,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

Expand Down Expand Up @@ -52,6 +53,7 @@ CREATE TABLE SchemeCriteria (
applicable_to VARCHAR(50),
marital_status marital_status_enum,
employment_status employment_status_enum,
age_unemployed_max INTEGER,
relationship VARCHAR(50),
age_min INTEGER,
age_max INTEGER
Expand Down
4 changes: 2 additions & 2 deletions src/controllers/applicants.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ const getApplicants = async (req, res, next) => {

const createApplicant = async (req, res, next) => {
try {
const { nric, name, sex, date_of_birth, marital_status, employment_status, household_members } = req.body;
const applicantData = { nric, name, sex, date_of_birth, marital_status, employment_status };
const { nric, name, sex, date_of_birth, marital_status, employment_status, date_unemployed, household_members } = req.body;
const applicantData = { nric, name, sex, date_of_birth, marital_status, employment_status, date_unemployed };
const newApplicant = await applicantModel.addNewApplicant(applicantData, household_members);
res.status(201).json(newApplicant);
} catch (err) {
Expand Down
11 changes: 7 additions & 4 deletions src/models/applicants.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const getApplicants = async (limit, offset) => {
TO_CHAR(a.date_of_birth, 'YYYY-MM-DD') AS applicant_date_of_birth,
a.marital_status,
a.employment_status,
a.date_unemployed,
a.created_at AS applicant_created_at,
json_agg(
json_build_object(
Expand Down Expand Up @@ -39,14 +40,14 @@ const addNewApplicant = async (applicant, household_members) => {
const client = await dbPool.connect();
await client.query('BEGIN'); // Begin transaction
try {
const { nric, name, sex, date_of_birth, marital_status, employment_status } = applicant;
const { nric, name, sex, date_of_birth, marital_status, employment_status, date_unemployed } = applicant;

const applicantResult = await client.query(
`INSERT INTO Applicants
(nric, name, sex, date_of_birth, marital_status, employment_status)
VALUES ($1, $2, $3, $4, $5, $6)
(nric, name, sex, date_of_birth, marital_status, employment_status, date_unemployed)
VALUES ($1, $2, $3, $4, $5, $6, $7)
RETURNING *`,
[nric, name, sex, date_of_birth, marital_status, employment_status]
[nric, name, sex, date_of_birth, marital_status, employment_status, date_unemployed]
);
const createdApplicant = applicantResult.rows[0];

Expand Down Expand Up @@ -82,6 +83,7 @@ const addNewApplicant = async (applicant, household_members) => {
date_of_birth: createdApplicant.date_of_birth,
marital_status: createdApplicant.marital_status,
employment_status: createdApplicant.employment_status,
date_unemployed: createdApplicant.date_unemployed,
created_at: createdApplicant.created_at,
household_members: householdMembers,
};
Expand All @@ -103,6 +105,7 @@ const getApplicantByID = async (id) => {
a.date_of_birth AS applicant_date_of_birth,
a.marital_status,
a.employment_status,
a.date_unemployed,
json_agg(
json_build_object(
'id', h.id,
Expand Down
3 changes: 3 additions & 0 deletions src/models/schemes.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const getSchemes = async (limit, offset) => {
'applicable_to', sc.applicable_to,
'marital_status', sc.marital_status,
'employment_status', sc.employment_status,
'age_unemployed_max', sc.age_unemployed_max,
'relationship', sc.relationship,
'age_min', sc.age_min,
'age_max', sc.age_max
Expand Down Expand Up @@ -58,6 +59,7 @@ const getActiveSchemes = async () => {
'applicable_to', sc.applicable_to,
'marital_status', sc.marital_status,
'employment_status', sc.employment_status,
'age_unemployed_max', sc.age_unemployed_max,
'relationship', sc.relationship,
'age_min', sc.age_min,
'age_max', sc.age_max
Expand Down Expand Up @@ -96,6 +98,7 @@ const getSchemeByID = async (id) => {
'applicable_to', sc.applicable_to,
'marital_status', sc.marital_status,
'employment_status', sc.employment_status,
'age_unemployed_max', sc.age_unemployed_max,
'relationship', sc.relationship,
'age_min', sc.age_min,
'age_max', sc.age_max
Expand Down
11 changes: 11 additions & 0 deletions src/specs/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,10 @@
"employment_status": {
"type": "string"
},
"date_unemployed": {
"type": "string",
"format": "date"
},
"created_at": {
"type": "string",
"format": "date-time"
Expand Down Expand Up @@ -466,6 +470,10 @@
"Unemployed"
]
},
"date_unemployed": {
"type": "string",
"format": "date"
},
"household_members": {
"type": "array",
"items": {
Expand Down Expand Up @@ -580,6 +588,9 @@
"employment_status": {
"type": "string"
},
"age_unemployed_max": {
"type": "integer"
},
"relationship": {
"type": "string"
},
Expand Down
33 changes: 32 additions & 1 deletion src/util.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@

// Implemented: Criteria1 && Criteria2

// Could be: CriteriaSet1 = Criteria1 || Criteria2 ...
// Eligibility = CriteriaSet1 && CriteriaSet2 ...


// conditions: {
// "marital_status":"married",
// "employment_status":"unemployed"
// },


const checkEligibility = (applicant, scheme) => {
const { marital_status, employment_status, household_members } = applicant;
const { marital_status, employment_status, date_unemployed, household_members } = applicant;

let isEligible = true;

Expand All @@ -11,6 +24,11 @@ const checkEligibility = (applicant, scheme) => {
if (criterion.employment_status && criterion.employment_status !== employment_status) {
isEligible = false;
}
const age_unemployed = calculateAgeInMonth(date_unemployed);
console.log("Applicant age unemployed", age_unemployed, criterion.age_unemployed_max);
if (criterion.age_unemployed_max && criterion.age_unemployed_max < age_unemployed) {
isEligible = false;
}
} else if (criterion.applicable_to === "Household member") {
const found = household_members.find(member => {
if (criterion.relationship && criterion.relationship !== member.relationship) {
Expand Down Expand Up @@ -39,6 +57,19 @@ const checkEligibility = (applicant, scheme) => {
return isEligible;
};

const calculateAgeInMonth = (dateInEffect) => {
if (!dateInEffect) {
return today.getFullYear() * 12;
}
const dateie = new Date(dateInEffect);
const today = new Date();

let age = today.getFullYear() - dateie.getFullYear();
const monthDiff = today.getMonth() - dateie.getMonth();

return age * 12 + monthDiff;
};

const calculateAge = (dateOfBirth) => {
const dob = new Date(dateOfBirth);
const today = new Date();
Expand Down

0 comments on commit 02d8140

Please sign in to comment.