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

Query optimisation #443

Merged
merged 4 commits into from
Dec 29, 2023
Merged
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
4 changes: 2 additions & 2 deletions src/controllers/v1/entity-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ module.exports = class Entity {
* @method
* @name update
* @param {Object} req - request data.
* @returns {JSON} - entities updation response.
* @returns {JSON} - entities updating response.
*/

async update(req) {
Expand Down Expand Up @@ -72,7 +72,7 @@ module.exports = class Entity {

async delete(req) {
try {
return await entityTypeService.delete(req.params.id)
return await entityTypeService.delete(req.params.id, req.decodedToken.organization_id)
} catch (error) {
return error
}
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/v1/questionsSet.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ module.exports = class QuestionsSet {

async read(req) {
try {
const questionsSetData = await questionsService.read(req.params.id)
const questionsSetData = await questionsService.read(req.params.id, req.body.code)
return questionsSetData
} catch (error) {
return error
Expand Down
73 changes: 37 additions & 36 deletions src/database/queries/entityType.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const EntityType = require('../models/index').EntityType
const Entity = require('../models/index').Entity
const { Op } = require('sequelize')
//const Sequelize = require('../database/models/index').sequelize
//const Sequelize = require('../models/index').sequelize

module.exports = class UserEntityData {
static async createEntityType(data) {
Expand All @@ -24,18 +24,11 @@ module.exports = class UserEntityData {
}
}

static async findAllEntityTypes(orgId, attributes, filter = {}) {
static async findAllEntityTypes(orgIds, attributes, filter = {}) {
try {
const entityData = await EntityType.findAll({
where: {
[Op.or]: [
{
created_by: 0,
},
{
organization_id: orgId,
},
],
organization_id: orgIds,
...filter,
},
attributes,
Expand All @@ -50,21 +43,24 @@ module.exports = class UserEntityData {
try {
const entityTypes = await EntityType.findAll({
where: filter,
raw: true,
})

const result = await Promise.all(
entityTypes.map(async (entityType) => {
const entities = await Entity.findAll({
where: { entity_type_id: entityType.id },
//attributes: { exclude: ['entity_type_id'] },
})
const entityTypeIds = entityTypes.map((entityType) => entityType.id)

return {
...entityType.toJSON(),
entities: entities.map((entity) => entity.toJSON()),
}
})
)
const entities = await Entity.findAll({
where: { entity_type_id: entityTypeIds, status: 'ACTIVE' },
raw: true,
//attributes: { exclude: ['entity_type_id'] },
})

const result = entityTypes.map((entityType) => {
const matchingEntities = entities.filter((entity) => entity.entity_type_id === entityType.id)
return {
...entityType,
entities: matchingEntities,
}
})

return result
} catch (error) {
Expand Down Expand Up @@ -99,11 +95,12 @@ module.exports = class UserEntityData {
}
} */

static async updateOneEntityType(id, update, options = {}) {
static async updateOneEntityType(id, orgId, update, options = {}) {
try {
return await EntityType.update(update, {
where: {
id: id,
organization_id: orgId,
},
...options,
})
Expand All @@ -112,11 +109,12 @@ module.exports = class UserEntityData {
}
}

static async deleteOneEntityType(id) {
static async deleteOneEntityType(id, organizationId) {
try {
return await EntityType.destroy({
where: {
id: id,
organization_id: organizationId,
},
individualHooks: true,
})
Expand All @@ -137,22 +135,25 @@ module.exports = class UserEntityData {
try {
const entityTypes = await EntityType.findAll({
where: filter,
raw: true,
})

const result = await Promise.all(
entityTypes.map(async (entityType) => {
const entities = await Entity.findAll({
where: { entity_type_id: entityType.id, status: 'ACTIVE' },
//attributes: { exclude: ['entity_type_id'] },
})
const entityTypeIds = entityTypes.map((entityType) => entityType.id)

return {
...entityType.toJSON(),
entities: entities.map((entity) => entity.toJSON()),
}
})
)
// Fetch all matching entities using the IDs
const entities = await Entity.findAll({
where: { entity_type_id: entityTypeIds, status: 'ACTIVE' },
raw: true,
//attributes: { exclude: ['entity_type_id'] },
})

const result = entityTypes.map((entityType) => {
const matchingEntities = entities.filter((entity) => entity.entity_type_id === entityType.id)
return {
...entityType,
entities: matchingEntities,
}
})
return result
} catch (error) {
return error
Expand Down
24 changes: 16 additions & 8 deletions src/services/entity-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ module.exports = class EntityHelper {
bodyData.updated_by = loggedInUserId
bodyData.organization_id = orgId
try {
const [updateCount, updatedEntityType] = await entityTypeQueries.updateOneEntityType(id, bodyData, {
const [updateCount, updatedEntityType] = await entityTypeQueries.updateOneEntityType(id, orgId, bodyData, {
returning: true,
raw: true,
})

if (updateCount === '0') {
if (updateCount === 0) {
return common.failureResponse({
message: 'ENTITY_TYPE_NOT_FOUND',
statusCode: httpStatusCode.bad_request,
Expand All @@ -76,7 +76,7 @@ module.exports = class EntityHelper {
} catch (error) {
if (error instanceof UniqueConstraintError) {
return common.failureResponse({
message: 'ENTITY_TYPE_ALREADY_DELETED',
message: 'ENTITY_TYPE_ALREADY_EXISTS',
statusCode: httpStatusCode.bad_request,
responseCode: 'CLIENT_ERROR',
})
Expand All @@ -85,11 +85,19 @@ module.exports = class EntityHelper {
}
}

static async readAllSystemEntityTypes(organization_id) {
static async readAllSystemEntityTypes(orgId) {
try {
const attributes = ['value', 'label', 'id']

const entities = await entityTypeQueries.findAllEntityTypes(organization_id, attributes)
const defaultOrgId = await getDefaultOrgId()
if (!defaultOrgId)
return common.failureResponse({
message: 'DEFAULT_ORG_ID_NOT_SET',
statusCode: httpStatusCode.bad_request,
responseCode: 'CLIENT_ERROR',
})

const entities = await entityTypeQueries.findAllEntityTypes([orgId, defaultOrgId], attributes)

if (!entities.length) {
return common.failureResponse({
Expand Down Expand Up @@ -146,10 +154,10 @@ module.exports = class EntityHelper {
* @returns {JSON} - Entity deleted response.
*/

static async delete(id) {
static async delete(id, organizationId) {
try {
const deleteCount = await entityTypeQueries.deleteOneEntityType(id)
if (deleteCount === '0') {
const deleteCount = await entityTypeQueries.deleteOneEntityType(id, organizationId)
if (deleteCount === 0) {
return common.failureResponse({
message: 'ENTITY_TYPE_NOT_FOUND',
statusCode: httpStatusCode.bad_request,
Expand Down
5 changes: 3 additions & 2 deletions src/services/mentees.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,11 +322,12 @@ module.exports = class MenteesHelper {

let validationData = await entityTypeQueries.findAllEntityTypesAndEntities({
status: 'ACTIVE',
allow_filtering: true,
})

let filteredQuery = utils.validateFilters(query, JSON.parse(JSON.stringify(validationData)), 'MentorExtension')
let filteredQuery = utils.validateFilters(query, validationData, sessionQueries.getModelName())

// Create saas fiter for view query
// Create saas filter for view query
const saasFilter = await this.filterSessionsBasedOnSaasPolicy(userId, isAMentor)

const sessions = await sessionQueries.getUpcomingSessionsFromView(
Expand Down
18 changes: 10 additions & 8 deletions src/services/mentors.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,6 @@ module.exports = class MentorsHelper {
*/
static async upcomingSessions(id, page, limit, search = '', menteeUserId, queryParams, isAMentor) {
try {
const query = utils.processQueryParametersWithExclusions(queryParams)
console.log(query)
let validationData = await entityTypeQueries.findAllEntityTypesAndEntities({
status: 'ACTIVE',
})
const filteredQuery = utils.validateFilters(query, JSON.parse(JSON.stringify(validationData)), 'sessions')

const mentorsDetails = await mentorQueries.getMentorExtension(id)
if (!mentorsDetails) {
return common.failureResponse({
Expand All @@ -48,6 +41,14 @@ module.exports = class MentorsHelper {
})
}

const query = utils.processQueryParametersWithExclusions(queryParams)

let validationData = await entityTypeQueries.findAllEntityTypesAndEntities({
status: 'ACTIVE',
allow_filtering: true,
})
const filteredQuery = utils.validateFilters(query, validationData, await sessionQueries.getModelName())

// Filter upcoming sessions based on saas policy
const saasFilter = await menteesService.filterSessionsBasedOnSaasPolicy(menteeUserId, isAMentor)

Expand Down Expand Up @@ -686,9 +687,10 @@ module.exports = class MentorsHelper {

let validationData = await entityTypeQueries.findAllEntityTypesAndEntities({
status: 'ACTIVE',
allow_filtering: true,
})

const filteredQuery = utils.validateFilters(query, JSON.parse(JSON.stringify(validationData)), 'Session')
const filteredQuery = utils.validateFilters(query, validationData, 'MentorExtension')
const userType = common.MENTOR_ROLE

const saasFilter = await this.filterMentorListBasedOnSaasPolicy(userId, isAMentor)
Expand Down
5 changes: 4 additions & 1 deletion src/services/questionsSet.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,14 @@ module.exports = class questionsSetHelper {
* @returns {JSON} - Read question set.
*/

static async read(questionsSetId) {
static async read(questionsSetId, questionSetCode) {
try {
const filter = {
id: questionsSetId,
}
if (questionSetCode) {
filter.code = questionSetCode
}
const questionSet = await questionsSetQueries.findOneQuestionsSet(filter)
if (!questionSet) {
return common.failureResponse({
Expand Down