Skip to content

Commit

Permalink
Merge pull request #140 from bcgov/scdicontact
Browse files Browse the repository at this point in the history
fixes for auth schools
  • Loading branch information
suzalflueck authored Dec 19, 2023
2 parents fc69cf1 + 7ea9316 commit 27ba0c4
Showing 1 changed file with 55 additions and 31 deletions.
86 changes: 55 additions & 31 deletions backend/src/routes/authority-router.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,41 +8,46 @@ const fs = require("fs");
const path = require("path");
const { checkToken } = require("../components/auth");
const { listCache } = require("../components/cache");
const {appendMailingAddressDetailsAndRemoveAddresses, rearrangeAndRelabelObjectProperties, sortByProperty} = require("../components/utils.js")
const {
appendMailingAddressDetailsAndRemoveAddresses,
rearrangeAndRelabelObjectProperties,
sortByProperty,
} = require("../components/utils.js");
//Batch Routes
router.get("/all-mailing/:type", checkToken, getAllAuthorityMailing);
router.get("/:id", checkToken, getAuthority);
//router.get("/all-contacts", checkToken, getAllAuthorityContacts);

async function getAllAuthorityMailing(req, res) {
//type = OFFSHORE or INDEPENDNT
const {type} = req.params
const { type } = req.params;

const params = [
{
condition: null,
searchCriteriaList: [
{
key: "closedDate",
operation: "eq",
value: null,
valueType: "STRING",
condition: "AND",
},
{
key: "authorityTypeCode",
operation: "eq",
value: type,
valueType: "STRING",
condition: "AND",
}
]
}]
const params = [
{
condition: null,
searchCriteriaList: [
{
key: "closedDate",
operation: "eq",
value: null,
valueType: "STRING",
condition: "AND",
},
{
key: "authorityTypeCode",
operation: "eq",
value: type,
valueType: "STRING",
condition: "AND",
},
],
},
];
const jsonString = JSON.stringify(params);
const encodedParams = encodeURIComponent(jsonString);
const url = await `${config.get(
"server:instituteAPIURL"
)}/institute/authority/paginated?pageSize=1000&sort[authorityNumber]=ASC&searchCriteriaList=${encodedParams}`
)}/institute/authority/paginated?pageSize=1000&sort[authorityNumber]=ASC&searchCriteriaList=${encodedParams}`;
try {
const authorityResponse = await axios.get(url, {
headers: { Authorization: `Bearer ${req.accessToken}` },
Expand All @@ -58,17 +63,24 @@ async function getAllAuthorityMailing(req, res) {
{ property: "phoneNumber", label: "Phone Number" },
{ property: "faxNumber", label: "Fax" },
{ property: "email", label: "Email" },

];

authorityResponse.data.content.forEach(appendMailingAddressDetailsAndRemoveAddresses);

authorityResponse.data.content.forEach(
appendMailingAddressDetailsAndRemoveAddresses
);

authorityResponse.data.content.forEach((currentElement, index, array) => {
const rearrangedElement = rearrangeAndRelabelObjectProperties(currentElement, propertyOrder);
const rearrangedElement = rearrangeAndRelabelObjectProperties(
currentElement,
propertyOrder
);
array[index] = rearrangedElement;
});
const authorityResponseSorted = sortByProperty(authorityResponse.data.content, 'Number')

const authorityResponseSorted = sortByProperty(
authorityResponse.data.content,
"Number"
);

res.json(authorityResponseSorted);
//res.json(districtContactsReorderedAndRelabeled );
} catch (e) {
Expand Down Expand Up @@ -123,7 +135,7 @@ async function getAuthority(req, res) {
)}/institute/authority/${id}`;
const authoritySchoolsUrl = `${config.get(
"server:instituteAPIURL"
)}/institute/school/paginated?pageNumber=0&pageSize=10&searchCriteriaList=${encodedParams}`;
)}/institute/school/paginated?pageNumber=0&pageSize=1000&searchCriteriaList=${encodedParams}`;

try {
const authorityDataResponse = await axios.get(url, {
Expand All @@ -133,9 +145,21 @@ async function getAuthority(req, res) {
const authoritySchoolsResponse = await axios.get(authoritySchoolsUrl, {
headers: { Authorization: `Bearer ${req.accessToken}` },
});
const today = new Date();
const filteredSchoolsResponse =
authoritySchoolsResponse.data.content.filter((obj) => {
// If closedDate is null, keep the object
if (obj.closedDate === null) {
return true;
}

// If closedDate is a valid date greater than today, keep the object
const closedDate = new Date(obj.closedDate);
return closedDate > today;
});
const authorityJSON = {
authorityData: authorityDataResponse.data,
authoritySchools: authoritySchoolsResponse.data.content,
authoritySchools: filteredSchoolsResponse,
};

res.json(authorityJSON);
Expand Down

0 comments on commit 27ba0c4

Please sign in to comment.