Skip to content

Commit

Permalink
Added sequelize transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
Deep Akabari committed Apr 19, 2024
1 parent eeeaabb commit 3cb34c4
Show file tree
Hide file tree
Showing 15 changed files with 249 additions and 35 deletions.
1 change: 1 addition & 0 deletions src/constants/message.constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ const messageConstant = {
FORM_FINALIZED: 'encounter form finalized successfully.',
FORM_FOUND: 'Encounter form already present.',
FORM_UPDATED: 'Encounter form updated.',
FORM_NOT_FOUND: 'Encounter form not found.',
FORM_NOT_FINALIZED: 'Encounter form not finalized',
DATA_RETRIEVED: 'Data retrieved successfully.',
INVALID_INPUT: 'Invalid id provided.',
Expand Down
16 changes: 16 additions & 0 deletions src/controllers/Admin/Access/access.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
import { Controller } from '../../../interfaces';
import dotenv from 'dotenv';
import { Op } from 'sequelize';
import { sequelize } from '../../../db/config/db.connection';

dotenv.config();

Expand Down Expand Up @@ -101,6 +102,7 @@ export const accountAccessByAccountType: Controller = async (req, res) => {
* @description This function is an Express controller that handles the creation of a new role for a specific account type. It first checks if a role with the provided name already exists. If it does, it sends a conflict error response. If not, it creates a new role with the provided name and account type, and sends a success response with the created role data.
*/
export const createRole: Controller = async (req, res) => {
const transaction = await sequelize.transaction();
try {
// Extract query parameters
const { roleName, accountType, permissionIds } = req.body;
Expand All @@ -110,6 +112,7 @@ export const createRole: Controller = async (req, res) => {

// If role already exists, return conflict response
if (existingRole) {
await transaction.rollback();
return res.status(httpCode.CONFLICT).json({
status: httpCode.CONFLICT,
message: messageConstant.ROLE_ALREADY_EXISTS,
Expand All @@ -134,13 +137,16 @@ export const createRole: Controller = async (req, res) => {
});
}

await transaction.commit();

// Return success response
return res.status(httpCode.OK).json({
status: httpCode.OK,
message: messageConstant.ROLE_CREATED,
data: createRole,
});
} catch (error) {
await transaction.rollback();
throw error;
}
};
Expand Down Expand Up @@ -264,12 +270,14 @@ export const viewRole: Controller = async (req, res) => {
* @description Updates the permissions associated with a specific role. It first removes all existing permissions and then adds the new permissions provided in the request body.
*/
export const updateRole: Controller = async (req, res) => {
const transaction = await sequelize.transaction();
try {
// Extract the role ID from the request parameters.
const { id } = req.params;

const existingRole = await Role.findByPk(id);
if (!existingRole) {
await transaction.rollback();
return res.status(httpCode.BAD_REQUEST).json({
status: httpCode.BAD_REQUEST,
message: messageConstant.INVALID_INPUT,
Expand Down Expand Up @@ -303,12 +311,15 @@ export const updateRole: Controller = async (req, res) => {
});
}

await transaction.commit();

// Send a success response with a status message.
return res.status(httpCode.OK).json({
status: httpCode.OK,
message: messageConstant.ROLE_UPDATED,
});
} catch (error) {
await transaction.rollback();
throw error;
}
};
Expand All @@ -321,12 +332,14 @@ export const updateRole: Controller = async (req, res) => {
* @description Deletes a specific role from the database based on the role ID provided in the request parameters.
*/
export const deleteRole: Controller = async (req, res) => {
const transaction = await sequelize.transaction();
try {
// Extract the role ID from the request parameters.
const { id } = req.params;

const existingRole = await Role.findByPk(id);
if (!existingRole) {
await transaction.rollback();
return res.status(httpCode.BAD_REQUEST).json({
status: httpCode.BAD_REQUEST,
message: messageConstant.INVALID_INPUT,
Expand All @@ -338,12 +351,15 @@ export const deleteRole: Controller = async (req, res) => {
where: { id }, // Specify the condition to find the role to delete.
});

await transaction.commit();

// Send a success response with a status message.
return res.status(httpCode.OK).json({
status: httpCode.OK,
message: messageConstant.ROLE_DELETED,
});
} catch (error) {
await transaction.rollback();
throw error;
}
};
2 changes: 1 addition & 1 deletion src/controllers/Admin/Common/common.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const getLoggedData: Controller = async (req, res) => {
});

// if there is no userDetails then give not found message
if (userDetails.length === 0) {
if (!userDetails.length) {
return res.status(httpCode.BAD_REQUEST).json({
status: httpCode.BAD_REQUEST,
message: messageConstant.DATA_NOT_FOUND,
Expand Down
43 changes: 42 additions & 1 deletion src/controllers/Admin/Dashboard/dashboard.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ import {
} from '../../../db/models/index';
import { Controller } from '../../../interfaces';
import transporter from '../../../utils/email';
import sequelize, { FindAttributeOptions, Includeable, Order } from 'sequelize';
import { FindAttributeOptions, Includeable, Order } from 'sequelize';
import { Op } from 'sequelize';
import { compileEmailTemplate } from '../../../utils/hbsCompiler';
import linkConstant from '../../../constants/link.constant';
import { sendSMS } from '../../../utils/smsSender';
import { sequelize } from '../../../db/config/db.connection'
import dotenv from 'dotenv';
dotenv.config();

Expand Down Expand Up @@ -414,6 +415,7 @@ export const viewNotes: Controller = async (req, res) => {
* @description This controller function updates the `adminNotes` field for a specific request identified by `id`. It uses the `Request.update` method to apply the changes to the database. Upon successful update, it sends back a response with a status code and a success message.
*/
export const updateNotes: Controller = async (req, res) => {
const transaction = await sequelize.transaction();
try {
// Extract id from request parameter
const { id } = req.params;
Expand All @@ -424,6 +426,7 @@ export const updateNotes: Controller = async (req, res) => {
// Check if request exists or not
const exists = await Request.findByPk(id);
if (!exists) {
await transaction.rollback();
return res.status(httpCode.BAD_REQUEST).json({
status: httpCode.BAD_REQUEST,
message: messageConstant.REQUEST_NOT_FOUND,
Expand All @@ -438,11 +441,14 @@ export const updateNotes: Controller = async (req, res) => {
{ where: { id } },
);

await transaction.commit();

return res.json({
status: httpCode.OK,
message: messageConstant.NOTE_UPDATED,
});
} catch (error) {
await transaction.rollback();
throw error;
}
};
Expand Down Expand Up @@ -513,6 +519,7 @@ export const getPatientData: Controller = async (req, res) => {
* @description This function is an Express controller that updates a case with the provided admin notes and cancellation reason, changes the status of the case to 'CancelledByAdmin', and sends the updated case data in the response.
*/
export const cancelCase: Controller = async (req, res) => {
const transaction = await sequelize.transaction();
try {
// Extract id from request parameter
const { id } = req.params;
Expand All @@ -526,6 +533,7 @@ export const cancelCase: Controller = async (req, res) => {
!exists ||
exists.requestStatus === RequestStatus.CancelledByAdmin
) {
await transaction.rollback();
return res.status(httpCode.BAD_REQUEST).json({
status: httpCode.BAD_REQUEST,
message: messageConstant.REQUEST_CANCELLED,
Expand All @@ -548,12 +556,15 @@ export const cancelCase: Controller = async (req, res) => {
},
);

await transaction.commit();

// success response
return res.status(httpCode.OK).json({
status: httpCode.OK,
message: messageConstant.CASE_CANCELLED,
});
} catch (error) {
await transaction.rollback();
throw error;
}
};
Expand All @@ -566,6 +577,7 @@ export const cancelCase: Controller = async (req, res) => {
* @description This function is an Express controller that updates a case with the provided description, changes the status of the case to 'Blocked'.
*/
export const blockCase: Controller = async (req, res) => {
const transaction = await sequelize.transaction();
try {
// Extract id from request parameter
const { id } = req.params;
Expand All @@ -576,6 +588,7 @@ export const blockCase: Controller = async (req, res) => {
// Check if request exists or not
const exists = await Request.findByPk(id);
if (!exists || exists.requestStatus === RequestStatus.Blocked) {
await transaction.rollback();
return res.status(httpCode.BAD_REQUEST).json({
status: httpCode.BAD_REQUEST,
message: messageConstant.REQUEST_BLOCKED,
Expand All @@ -592,12 +605,15 @@ export const blockCase: Controller = async (req, res) => {
{ where: { id } },
);

await transaction.commit();

// success response
return res.status(httpCode.OK).json({
status: httpCode.OK,
message: messageConstant.CASE_BLOCKED,
});
} catch (error) {
await transaction.rollback();
throw error;
}
};
Expand All @@ -610,12 +626,14 @@ export const blockCase: Controller = async (req, res) => {
* @description This function is an Express controller that updates the status of a case to 'Cleared'.
*/
export const clearCase: Controller = async (req, res) => {
const transaction = await sequelize.transaction();
try {
const { id } = req.params;

// Check if request exists or not
const exists = await Request.findByPk(id);
if (!exists || exists.requestStatus === RequestStatus.Cleared) {
await transaction.rollback();
return res.status(httpCode.BAD_REQUEST).json({
status: httpCode.BAD_REQUEST,
message: messageConstant.REQUEST_CLEARED,
Expand All @@ -634,11 +652,14 @@ export const clearCase: Controller = async (req, res) => {
},
);

await transaction.commit();

return res.status(httpCode.OK).json({
status: httpCode.OK,
message: messageConstant.CASE_CLEARED,
});
} catch (error) {
await transaction.rollback();
throw error;
}
};
Expand Down Expand Up @@ -872,6 +893,7 @@ export const getPhysicianByRegion: Controller = async (req, res) => {
* @description This function is an Express controller that assigns a case based on the provided physicianId, admin note and update physicianId and adminNotes and caseTag: Pending.
*/
export const assignCase: Controller = async (req, res) => {
const transaction = await sequelize.transaction();
try {
// Extract request parameters and body data
const { id } = req.params;
Expand All @@ -880,6 +902,7 @@ export const assignCase: Controller = async (req, res) => {
// Check if request exists or not
const exists = await Request.findByPk(id);
if (!exists) {
await transaction.rollback();
// If request doesn't exist, return bad request response
return res.status(httpCode.BAD_REQUEST).json({
status: httpCode.BAD_REQUEST,
Expand All @@ -896,12 +919,15 @@ export const assignCase: Controller = async (req, res) => {
{ where: { id } },
);

await transaction.commit();

// Return success response
return res.status(httpCode.OK).json({
status: httpCode.OK,
message: messageConstant.CASE_ASSIGNED,
});
} catch (error) {
await transaction.rollback();
throw error;
}
};
Expand Down Expand Up @@ -1206,6 +1232,7 @@ export const closeCaseView: Controller = async (req, res) => {
* @description This function is an Express controller that updates the close case.
*/
export const updateCloseCase: Controller = async (req, res) => {
const transaction = await sequelize.transaction();
try {
// Extract the request id from request parameters.
const { id } = req.params;
Expand All @@ -1216,6 +1243,7 @@ export const updateCloseCase: Controller = async (req, res) => {
// Check if request exists or not
const exists = await Request.findByPk(id);
if (!exists) {
await transaction.rollback();
return res.status(httpCode.BAD_REQUEST).json({
status: httpCode.BAD_REQUEST,
message: messageConstant.REQUEST_NOT_FOUND,
Expand All @@ -1231,11 +1259,14 @@ export const updateCloseCase: Controller = async (req, res) => {
{ where: { id } },
);

await transaction.commit();

return res.status(httpCode.OK).json({
status: httpCode.OK,
message: messageConstant.CLOSE_CASE_UPDATED,
});
} catch (error) {
await transaction.rollback();
throw error;
}
};
Expand All @@ -1248,12 +1279,14 @@ export const updateCloseCase: Controller = async (req, res) => {
* @description This controller function updates the status of a case to 'Closed' and its case tag to 'UnPaid'. It is triggered when a case with the tag 'Close' needs to be updated to reflect its closure. The function responds with a success message upon successful update.
*/
export const closeCase: Controller = async (req, res) => {
const transaction = await sequelize.transaction();
try {
const { id } = req.params;

// Check if request exists or not
const exists = await Request.findByPk(id);
if (!exists) {
await transaction.rollback();
return res.status(httpCode.BAD_REQUEST).json({
status: httpCode.BAD_REQUEST,
message: messageConstant.REQUEST_NOT_FOUND,
Expand All @@ -1265,11 +1298,14 @@ export const closeCase: Controller = async (req, res) => {
{ where: { id, caseTag: CaseTag.Close } },
);

await transaction.commit();

return res.status(httpCode.OK).json({
status: httpCode.OK,
message: messageConstant.CASE_CLOSED,
});
} catch (error) {
await transaction.rollback();
throw error;
}
};
Expand Down Expand Up @@ -1360,6 +1396,7 @@ export const requestSupport: Controller = async (req, res) => {
* @description This function is an Express controller that handles request transfer. It updates the `physicianId` and `transferNote` of a request with the given `id` and sends a success response.
*/
export const transferRequest: Controller = async (req, res) => {
const transaction = await sequelize.transaction();
try {
// Extract Request id from request parameters.
const { id } = req.params;
Expand All @@ -1368,6 +1405,7 @@ export const transferRequest: Controller = async (req, res) => {
// Check if request exists or not
const exists = await Request.findByPk(id);
if (!exists) {
await transaction.rollback();
return res.status(httpCode.BAD_REQUEST).json({
status: httpCode.BAD_REQUEST,
message: messageConstant.REQUEST_NOT_FOUND,
Expand All @@ -1376,11 +1414,14 @@ export const transferRequest: Controller = async (req, res) => {

await Request.update({ physicianId, transferNote }, { where: { id } });

await transaction.commit();

return res.status(httpCode.OK).json({
status: httpCode.OK,
message: messageConstant.REQUEST_TRANSFERRED,
});
} catch (error) {
await transaction.rollback();
throw error;
}
};
Expand Down
Loading

0 comments on commit 3cb34c4

Please sign in to comment.