Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes for auth schools #140

Merged
merged 1 commit into from
Dec 19, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading