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

[feature]: Add/Modify endpoints to allow query of disabled records #511

Open
wants to merge 14 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion __test__/api/applications/getSingleApplicationLog.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe("Get single application logs", () => {
let app, request;

it("should return all logs for a given application", async () => {
process.env.LOGGING_ENABLED = true;
process.env.loggingEnabled = true;

//create express app
app = express();
Expand Down
46 changes: 46 additions & 0 deletions __test__/api/msadmins/getAllMsAdmins.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const app = require("../../../server");
const supertest = require("supertest");
const MsAdmin = require("../../../models/msadmins");
const request = supertest(app);
const { deleteById, restoreById } = require("../../../utils/softDelete");

// Cached admin and allMsAdminsResponse.
let msAdmin, msAdmin2, msAdmin3, allMsAdminsResponse;
Expand Down Expand Up @@ -44,18 +45,21 @@ describe("GET /msAdmins", () => {
fullname: msAdmin.fullname,
email: msAdmin.email,
role: msAdmin.role,
isDisabled: false,
},
{
msAdminId: msAdmin2.id,
fullname: msAdmin2.fullname,
email: msAdmin2.email,
role: msAdmin2.role,
isDisabled: false,
},
{
msAdminId: msAdmin3.id,
fullname: msAdmin3.fullname,
email: msAdmin3.email,
role: msAdmin3.role,
isDisabled: false,
},
];
});
Expand Down Expand Up @@ -179,4 +183,46 @@ describe("GET /msAdmins", () => {
expect(res.body.status).toEqual("error");
expect(res.body.data).toEqual([]);
});

it("Should get all disabled microservice admins with all pagination params set", async () => {
const url = `/v1/msAdmins`;
const bearerToken = `bearer ${global.superSysToken}`;

//disable one admin
await deleteById(MsAdmin, msAdmin2.id, global.msSuperAdmin.id);

const res = await request
.get(url)
.query({ limit: 3, page: 1, sort: "asc", filter: "disabled" })
.set("Authorization", bearerToken);

// const expectedValue = allMsAdminsResponse.slice(0, 1);

expect(res.status).toEqual(200);
expect(res.body.status).toEqual("success");
expect(res.body.data.records.length).toEqual(1);

await restoreById(MsAdmin, msAdmin2.Id);
});

it("Should get both disabled/enabled microservice admins with all pagination params set", async () => {
const url = `/v1/msAdmins`;
const bearerToken = `bearer ${global.superSysToken}`;

//disable one admin
await deleteById(MsAdmin, msAdmin2.id, global.msSuperAdmin.id);

const res = await request
.get(url)
.query({ limit: 3, page: 1, sort: "asc", filter: "all" })
.set("Authorization", bearerToken);

// const expectedValue = allMsAdminsResponse.slice(0, 1);

expect(res.status).toEqual(200);
expect(res.body.status).toEqual("success");
expect(res.body.data.records.length).toEqual(3);

await restoreById(MsAdmin, msAdmin2.Id);
});
});
4 changes: 4 additions & 0 deletions __test__/api/msadmins/settings/updateSystemSettings.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ describe("PATCH /msAdmins/settings", () => {
maxItemsPerPage: 50,
defaultMaxRequestsPerDay: 10000,
disableRequestLimits: false,
loggingEnabled: true,
logPageSize: 100,
};

let res = await request
Expand All @@ -46,6 +48,8 @@ describe("PATCH /msAdmins/settings", () => {
maxItemsPerPage: 20,
defaultMaxRequestsPerDay: 10000,
disableRequestLimits: false,
loggingEnabled: true,
logPageSize: 100,
};

//check for update also in same process because this is a single document collection
Expand Down
10 changes: 5 additions & 5 deletions __test__/middleware/requestLogger.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ describe("Logger middleware", () => {
let app, request;

it("logging is system-enabled and app-enabled for all requests", async () => {
process.env.LOGGING_ENABLED = true;
process.env.loggingEnabled = true;
app = express();
app.use((req, res, next) => {
const logging = {
Expand Down Expand Up @@ -79,7 +79,7 @@ describe("Logger middleware", () => {
});

it("logging is system-disabled", async () => {
process.env.LOGGING_ENABLED = false;
process.env.loggingEnabled = false;
app = express();
app.use((req, res, next) => {
const logging = {
Expand Down Expand Up @@ -115,7 +115,7 @@ describe("Logger middleware", () => {
});

it("logging is not available for application", async () => {
process.env.LOGGING_ENABLED = true;
process.env.loggingEnabled = true;
app = express();
app.use((req, res, next) => {
const logging = {};
Expand Down Expand Up @@ -151,7 +151,7 @@ describe("Logger middleware", () => {
});

it("logging is system-enabled and app-enabled for all requests but skipped status 400", async () => {
process.env.LOGGING_ENABLED = true;
process.env.loggingEnabled = true;
app = express();
app.use((req, res, next) => {
const logging = {
Expand Down Expand Up @@ -207,7 +207,7 @@ describe("Logger middleware", () => {
});

it("logging is system-disabled when env variable is undefined", async () => {
process.env.LOGGING_ENABLED = "";
process.env.loggingEnabled = "";
app = express();
app.use((req, res, next) => {
const logging = {
Expand Down
69 changes: 69 additions & 0 deletions __test__/utils/paginateDeleted.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
const { paginateDeleted } = require("../../utils/paginateDeleted");
const softDelete = require("../../utils/softDelete");
const MsAdmin = require("../../models/msadmins");

describe("Paginete soft-deleted records", () => {
let msAdmin,
idsArray = [];
beforeAll(async () => {
//create dummy records and softDelete them
for (let index = 0; index < 20; index++) {
msAdmin = new MsAdmin({
fullname: "paginateDeleteTest",
email: `admin${Date.now()}@domain.com`,
password: "some password",
});
await msAdmin.save();

idsArray.push(msAdmin.id);

//soft delete
if ((index + 2) % 2 === 0) {
await softDelete.deleteById(MsAdmin, msAdmin.id, global.msAdmin.id);
}
}
});

afterAll(async () => {
for (let index = 0; index < idsArray.length; index++) {
const msAdminId = idsArray[index];
await softDelete.restoreById(MsAdmin, msAdminId);
await MsAdmin.findByIdAndDelete(msAdminId);
}
});

it("should get all deleted records", async () => {
const deletedAdmins = await paginateDeleted(
MsAdmin,
"disabled",
{ fullname: "paginateDeleteTest" },
5,
1
);
expect(deletedAdmins.totalRecords).toEqual(10);
});

it("should get all undeleted records", async () => {
const unDeleted = await paginateDeleted(
MsAdmin,
"enabled",
{ fullname: "paginateDeleteTest" },
5,
1
);
expect(unDeleted.totalRecords).toEqual(10);
});

it("should get all records", async () => {
const allAdmins = await paginateDeleted(
MsAdmin,
"all",
{ fullname: "paginateDeleteTest" },
5,
1
);
expect(allAdmins.totalRecords).toEqual(20);
});

//retrieve the deleted records by page
});
4 changes: 2 additions & 2 deletions controllers/applicationsController/getSingleApplicationLog.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ const getSingleApplicationLog = async (req, res, next) => {

//reuse this controller for msAdmins, they can find any application
if (req.token.msAdminId) {
const application = Application.findById(applicationId);
const application = await Application.findById(applicationId);
if (!application) {
return next(new CustomError(404, "Application not found"));
}
} else {
//check if requested application belongs to organization of admin user
const application = Application.find({
const application = await Application.find({
_id: applicationId,
organizationId,
});
Expand Down
24 changes: 20 additions & 4 deletions controllers/msAdminsController/applications/getAllApplications.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ const Applications = require("../../../models/applications");
const MsAdmin = require("../../../models/msadmins");
const CustomError = require("../../../utils/customError");
const responseHandler = require("../../../utils/responseHandler");
const {
getAllRecords,
getDeletedRecords,
} = require("../../../utils/softDelete");

/**
* @author Ekeyekwu Oscar
Expand All @@ -20,6 +24,7 @@ const getAllApplications = async (req, res, next) => {
let allApplications;

let query = {};
const { page, limit } = req.paginateOptions;

try {
//check if msAdmin exists
Expand All @@ -29,18 +34,29 @@ const getAllApplications = async (req, res, next) => {
return;
}

const { filter } = req.query;

//get all applications
const applications = await Applications.paginate(query, {
...req.paginateOptions,
populate: "organizationId",
});
let applications;

if (filter === "disabled") {
applications = await getDeletedRecords(Applications, page, limit);
} else if (filter === "all") {
applications = await getAllRecords(Applications, page, limit);
} else {
applications = await Applications.paginate(query, {
...req.paginateOptions,
populate: "organizationId",
});
}

allApplications = applications.docs.map((application) => {
return {
applicationId: application._id,
applicationName: application.name,
organizationId: application.organizationId,
organizationName: application.organizationId.name,
isBlocked: application.deleted || false,
};
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,35 +29,21 @@ const getSingleApplication = async (req, res, next) => {
}

//get applications
const singleApplication = await Applications.findById(
applicationId
).populate("organizationId");
const singleApplication = await Applications.findOneWithDeleted({
_id: applicationId,
}).populate("organizationId");

if (!singleApplication) {
next(new CustomError(404, "Application not Found"));
return;
}

// let commentsCount, repliesCount;
// const comments = await CommentModel.find({applicationId:applicationId});
// console.log(comments)
// if (!comments || comments === []) {
// commentsCount = 0;
// repliesCount = 0;
// } else {
// commentsCount = comments.records.length;
// repliesCount = comments.records.reduce((acc, curr) => {
// return acc + curr.numOfReplies;
// }, 0);
// }
// console.log(commentsCount, repliesCount);
application = {
applicationId: singleApplication._id,
applicationName: singleApplication.name,
organizationId: singleApplication.organizationId,
organizationName: singleApplication.organizationId.name,
// totalNumOfComments: commentsCount,
// totalNumOfReplies: repliesCount,
isBlocked: singleApplication.deleted || false,
};
} catch (error) {
next(new CustomError(400, "An error occured retrieving Application"));
Expand Down
14 changes: 13 additions & 1 deletion controllers/msAdminsController/getAllMsAdmins.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const MsAdmin = require("../../models/msadmins");
const CustomError = require("../../utils/customError");
const responseHandler = require("../../utils/responseHandler");
const { getAllRecords, getDeletedRecords } = require("../../utils/softDelete");

/**
* @author David Okanlawon
Expand All @@ -14,20 +15,31 @@ const responseHandler = require("../../utils/responseHandler");
const getAllMsAdmins = async (req, res, next) => {
//get all admins mapping the field names appropriately
let admins, allMsAdmins;
const { filter } = req.query;

let query = {};
const { page, limit } = req.paginateOptions;

try {
admins = await MsAdmin.paginate(query, req.paginateOptions);
if (filter === "disabled") {
admins = await getDeletedRecords(MsAdmin, page, limit);
} else if (filter === "all") {
admins = await getAllRecords(MsAdmin, page, limit);
} else {
admins = await MsAdmin.paginate(query, req.paginateOptions);
}

allMsAdmins = admins.docs.map((admin) => {
return {
msAdminId: admin._id,
fullname: admin.fullname,
email: admin.email,
role: admin.role,
isDisabled: admin.deleted || false,
};
});
} catch (error) {
console.log(error.message);
next(new CustomError(400, "An error occured retrieving admin accounts"));
return;
}
Expand Down
3 changes: 2 additions & 1 deletion controllers/msAdminsController/getSingleMsAdmin.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const getSingleMsAdmin = async (req, res, next) => {

//get msAdmin account
try {
const msAdmin = await MsAdmin.findById(msAdminId);
const msAdmin = await MsAdmin.findOneWithDeleted({ _id: msAdminId });
if (!msAdmin) {
next(new CustomError(404, "MsAdmin account not found"));
return;
Expand All @@ -25,6 +25,7 @@ const getSingleMsAdmin = async (req, res, next) => {
fullname: msAdmin.fullname,
email: msAdmin.email,
role: msAdmin.role,
isDisabled: msAdmin.deleted || false,
};

return responseHandler(
Expand Down
Loading