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

rollout db changes #255

Merged
merged 11 commits into from
Nov 29, 2024
7 changes: 7 additions & 0 deletions src/constants/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ function getResourceActions(resource) {
RESOURCE_REPORTED: `${resource}_REJECTED_AND_REPORTED`,
RESOURCE_REJECTED: `${resource}_REJECTED`,
REVIEW_INPROGRESS: `${resource}_REVIEW_INPROGRESS`,
ROLLOUT_CREATED: `CREATE_${resource}`,
ROLLOUT_DELETED: `DELETE_${resource}`,
ROLLOUT_PUBLISHED: `${resource}_PUBLISHED`,
}
}

Expand Down Expand Up @@ -168,9 +171,12 @@ module.exports = {
},
MODEL_NAMES: {
RESOURCE: 'Resource',
ROLLOUT: 'Rollouts',
},
USER_ACTIONS: {
project: getResourceActions('PROJECT'),
rollout_program: getResourceActions('PROGRAM_ROLLOUT'),
rollout_solution: getResourceActions('SOLUTION_ROLLOUT'),
},
EVENT_ADD_USER_ACTION: 'addUserAction',
REQUEST_TIMEOUT_MS: 3000,
Expand All @@ -195,4 +201,5 @@ module.exports = {
RECOMMENDED_DURATION: 'recommended_duration',
NUMBER: 'number',
STRING: 'string',
ROLLOUT_STATUS_PUBLISHED: 'PUBLISHED',
}
106 changes: 106 additions & 0 deletions src/database/migrations/20241122100559-create-table-rollouts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
'use strict'

/** @type {import('sequelize-cli').Migration} */
module.exports = {
async up(queryInterface, Sequelize) {
await queryInterface.createTable('rollouts', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER,
},
priyanka-TL marked this conversation as resolved.
Show resolved Hide resolved
resource_type: {
adithyadinesh0412 marked this conversation as resolved.
Show resolved Hide resolved
allowNull: false,
type: Sequelize.STRING,
},
resource_id: {
adithyadinesh0412 marked this conversation as resolved.
Show resolved Hide resolved
allowNull: false,
type: Sequelize.INTEGER,
},
status: {
adithyadinesh0412 marked this conversation as resolved.
Show resolved Hide resolved
allowNull: false,
type: Sequelize.ENUM('PENDING', 'ROLLED_OUT', 'INACTIVE'),
defaultValue: 'PENDING',
},
published_on: {
allowNull: true,
type: Sequelize.DATE,
},
organization_id: {
primaryKey: true,
allowNull: false,
type: Sequelize.STRING,
},
user_id: {
adithyadinesh0412 marked this conversation as resolved.
Show resolved Hide resolved
allowNull: false,
type: Sequelize.STRING,
},
start_date: {
allowNull: false,
type: Sequelize.DATE,
},
end_date: {
allowNull: false,
type: Sequelize.DATE,
},
published_id: {
type: Sequelize.STRING,
},
title: {
allowNull: false,
type: Sequelize.STRING,
},
blob_path: {
adithyadinesh0412 marked this conversation as resolved.
Show resolved Hide resolved
allowNull: true,
type: Sequelize.STRING,
},
parent_id: {
allowNull: false,
defaultValue: 0,
type: Sequelize.INTEGER,
},
type: {
allowNull: false,
type: Sequelize.ENUM('program', 'solution'),
defaultValue: 'program',
},
template_id: {
type: Sequelize.STRING,
},
created_by: {
allowNull: false,
type: Sequelize.STRING,
},
updated_by: {
allowNull: false,
type: Sequelize.STRING,
},
created_at: {
allowNull: false,
type: Sequelize.DATE,
},
updated_at: {
allowNull: false,
type: Sequelize.DATE,
},
deleted_at: {
type: Sequelize.DATE,
},
})

await queryInterface.addIndex('rollouts', ['status'], {
name: 'rollouts_status_index',
})
await queryInterface.addIndex('rollouts', ['user_id'], {
adithyadinesh0412 marked this conversation as resolved.
Show resolved Hide resolved
name: 'rollouts_user_id_index',
})
await queryInterface.addIndex('rollouts', ['title'], {
name: 'rollouts_title_index',
})
},

async down(queryInterface, Sequelize) {
await queryInterface.dropTable('rollouts')
},
}
64 changes: 64 additions & 0 deletions src/database/migrations/20241125122503-add-activites-rollout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
'use strict'

/** @type {import('sequelize-cli').Migration} */
module.exports = {
up: async (queryInterface, Sequelize) => {
const actionsData = [
{
code: 'CREATE_PROGRAM_ROLLOUT',
description: 'created the Program rollout with',
},
{
adithyadinesh0412 marked this conversation as resolved.
Show resolved Hide resolved
code: 'CREATE_SOLUTION_ROLLOUT',
description: 'created the Solution rollout with',
},
{
code: 'DELETE_PROGRAM_ROLLOUT',
description: 'deleted the Program rollout with',
},
{
code: 'DELETE_SOLUTION_ROLLOUT',
description: 'deleted the Solution rollout with',
},
{
code: 'PROGRAM_ROLLOUT_PUBLISHED',
description: 'published the Program rollout with',
},
{
code: 'SOLUTION_ROLLOUT_PUBLISHED',
description: 'published the Solution rollout with',
},
]

const actionsFinalArray = actionsData.map((action) => {
const timestamp = new Date()
return {
...action,
created_at: timestamp,
updated_at: timestamp,
}
})

// Insert the data into the 'actions' table
await queryInterface.bulkInsert('actions', actionsFinalArray)
},

down: async (queryInterface, Sequelize) => {
await queryInterface.bulkDelete(
'actions',
{
code: {
[Sequelize.Op.in]: [
'CREATE_PROGRAM_ROLLOUT',
'CREATE_SOLUTION_ROLLOUT',
'DELETE_PROGRAM_ROLLOUT',
'DELETE_SOLUTION_ROLLOUT',
'PROGRAM_ROLLOUT_PUBLISHED',
'SOLUTION_ROLLOUT_PUBLISHED',
],
},
},
{}
)
},
}
116 changes: 116 additions & 0 deletions src/database/models/rollouts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
const common = require('@constants/common')

module.exports = (sequelize, DataTypes) => {
const Rollout = sequelize.define(
'Rollout',
{
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: DataTypes.INTEGER,
},
resource_type: {
allowNull: false,
type: DataTypes.STRING,
},
resource_id: {
adithyadinesh0412 marked this conversation as resolved.
Show resolved Hide resolved
allowNull: false,
type: DataTypes.INTEGER,
},
status: {
allowNull: false,
type: DataTypes.ENUM('PENDING', 'ROLLED_OUT', 'INACTIVE'),
defaultValue: 'PENDING',
},
published_on: {
allowNull: true,
type: DataTypes.DATE,
},
organization_id: {
primaryKey: true,
allowNull: false,
type: DataTypes.STRING,
},
user_id: {
allowNull: false,
type: DataTypes.STRING,
},
start_date: {
allowNull: false,
type: DataTypes.DATE,
},
end_date: {
allowNull: false,
type: DataTypes.DATE,
},
published_id: {
type: DataTypes.STRING,
},
title: {
allowNull: false,
type: DataTypes.STRING,
},
blob_path: {
allowNull: true,
type: DataTypes.STRING,
},
parent_id: {
type: DataTypes.INTEGER,
},
type: {
allowNull: true,
type: DataTypes.ENUM('program', 'solution'),
},
template_id: {
type: DataTypes.STRING,
},
created_by: {
allowNull: false,
type: DataTypes.STRING,
},
updated_by: {
type: DataTypes.STRING,
},
},
{
modelName: 'Rollout',
tableName: 'rollouts',
freezeTableName: true,
paranoid: true,
}
)
// Helper function to emit user actions with dynamic action types
const emitUserAction = async (instance, actionType) => {
console.log(actionType, 'actionType')
// CREATE_PROGRAM_ROLLOUT
// rollout_program
// console.log(common.USER_ACTIONS['rollout_' + instance.resource_type.toLowerCase()][actionType], 'jjj')
try {
if (actionType) {
eventEmitter.emit(common.EVENT_ADD_USER_ACTION, {
actionCode: common.USER_ACTIONS['rollout_' + instance.type.toLowerCase()][actionType],
userId: instance.user_id,
objectId: instance.id,
objectType: common.MODEL_NAMES.ROLLOUT,
orgId: instance.organization_id,
})
}
} catch (error) {
console.error(`Error during ${actionType} hook:`, error)
throw error
}
}

Rollout.addHook('afterCreate', (instance) => emitUserAction(instance, `ROLLOUT_CREATE`))

Rollout.addHook('afterDestroy', (instance) => emitUserAction(instance, 'ROLLOUT_DELETED'))

Rollout.addHook('afterUpdate', (instance) => {
if (instance.status == common.ROLLOUT_STATUS_PUBLISHED) {
emitUserAction(instance, 'ROLLOUT_PUBLISHED')
}
})

return Rollout
}
3 changes: 2 additions & 1 deletion src/distributionColumns.psql
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ SELECT create_distributed_table('resource_creator_mapping', 'creator_id');
SELECT create_distributed_table('review_resources', 'reviewer_id');
SELECT create_distributed_table('review_stages', 'organization_id');
SELECT create_distributed_table('reviews', 'organization_id');
SELECT create_distributed_table('activities', 'object_id');
SELECT create_distributed_table('activities', 'object_id');
SELECT create_distributed_table('rollouts', 'organization_id');