Skip to content

Commit

Permalink
Update seeder and add created and updated by user of user model.
Browse files Browse the repository at this point in the history
  • Loading branch information
HranikBs23 committed Feb 23, 2022
1 parent 6e0a90e commit 7e31156
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 19 deletions.
14 changes: 8 additions & 6 deletions seeder.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ async function init() {
User.findOrCreate({
where: { email: "[email protected]" },
defaults: {
id: "479753f6-dfeb-47da-abf4-6b41332842a0",
first_name: "System",
last_name: "Admin",
password: "P@ssword123",
Expand All @@ -50,6 +51,7 @@ async function init() {
(admin) => {
const profiles = [
{
id: "1da2bb9b-9594-42f4-ae3d-f543756e9430",
title: "System Admin",
slug: "system-admin",
type: 'standard',
Expand Down Expand Up @@ -92,11 +94,11 @@ async function init() {
function serviceSeeder(callback) {
User.findOne({ where: { email: '[email protected]' } }).then(admin => {
const services = [
{ title: "Management of Platform", slug: "platform-management", created_by: admin.id, updated_by: admin.id },
{ title: "Manage Users", slug: "manage-users", description: 'Manage CDP users', created_by: admin.id, updated_by: admin.id },
{ title: "Manage Profiles", slug: "manage-profiles", description: 'Manage user profiles', created_by: admin.id, updated_by: admin.id },
{ title: "Manage Roles", slug: "manage-roles", description: 'Manage user roles', created_by: admin.id, updated_by: admin.id },
{ title: "Manage Permissions", slug: "manage-permissions", description: 'Assign rights to Permission', created_by: admin.id, updated_by: admin.id },
{ id: "6f8cb12c-c3a6-44c6-b1af-9f0a6c8cb401", title: "Management of Platform", slug: "platform-management", created_by: admin.id, updated_by: admin.id },
{ id: "79f26ef2-52f9-429e-b4bf-c307aa0a6040", title: "Manage Users", slug: "manage-users", description: 'Manage CDP users', created_by: admin.id, updated_by: admin.id },
{ id: "7ae9a3ba-132e-407b-8b0f-bc31bd8b52c1", title: "Manage Profiles", slug: "manage-profiles", description: 'Manage user profiles', created_by: admin.id, updated_by: admin.id },
{ id: "6d1d48c5-565c-4b3b-b4f5-5ad424070333", title: "Manage Roles", slug: "manage-roles", description: 'Manage user roles', created_by: admin.id, updated_by: admin.id },
{ id: "bd690286-de0a-416d-9d04-fda113fb9366", title: "Manage Permissions", slug: "manage-permissions", description: 'Assign rights to Permission', created_by: admin.id, updated_by: admin.id },
];

Service.destroy({ truncate: { cascade: true } }).then(() => {
Expand All @@ -113,7 +115,7 @@ async function init() {
function permissionSeeder(callback) {
User.findOne({ where: { email: '[email protected]' } }).then(admin => {
const permission = [
{ title: "System Admin Permission", slug: "system-admin", type: 'standard', description: "This is the default permission set for System Admin", created_by: admin.id, updated_by: admin.id },
{ id: "bffd87d0-3843-4e5f-b564-0edf6b208149", title: "System Admin Permission", slug: "system-admin", type: 'standard', description: "This is the default permission set for System Admin", created_by: admin.id, updated_by: admin.id },
];

Permission.destroy({ truncate: { cascade: true } }).then(() => {
Expand Down
48 changes: 35 additions & 13 deletions src/modules/platform/user/user.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,15 @@ async function getSignedInUserProfile (req, res) {
]
}
]
}
},
{
model: User,
as: "createdByUser",
},
{
model: User,
as: "updatedByUser",
},
],
});

Expand Down Expand Up @@ -202,6 +210,14 @@ async function getUsers(req, res) {
model: Role,
as: "role",
},
{
model: User,
as: "createdByUser",
},
{
model: User,
as: "updatedByUser",
},
],
});

Expand Down Expand Up @@ -287,7 +303,15 @@ async function getUser(req, res) {
]
}
]
}
},
{
model: User,
as: "createdByUser",
},
{
model: User,
as: "updatedByUser",
},
],
});

Expand All @@ -302,7 +326,6 @@ async function getUser(req, res) {

async function createUser(req, res) {
try {
const loggedUser = req.user;
const { first_name, last_name, email, password, profile_id, role_id } = req.body;

const existUser = await User.findOne({
Expand Down Expand Up @@ -337,8 +360,8 @@ async function createUser(req, res) {
password,
profile_id: profile_id,
role_id: role?.id || null,
created_by: loggedUser.id,
updated_by: loggedUser.id,
created_by: req.user.id,
updated_by: req.user.id,
});


Expand All @@ -354,7 +377,6 @@ async function updateUser(req, res) {
try {
const { id } = req.params;
const { first_name, last_name, email, profile_id, role_id } = req.body;
const userId = req.user.id;

const user = await User.findOne({
where: {
Expand All @@ -364,8 +386,8 @@ async function updateUser(req, res) {

if (!user) return res.status(404).send("User not found!");

if (first_name) user.update({ first_name, updated_by: userId });
if (last_name) user.update({ last_name, updated_by: userId });
if (first_name) user.update({ first_name, updated_by: req.user.id });
if (last_name) user.update({ last_name, updated_by: req.user.id });
if (email) {
const existingUser = await User.findOne({
where: {
Expand All @@ -374,7 +396,7 @@ async function updateUser(req, res) {
});
if (existingUser) return res.status(400).send("Already registered with this email address.");

user.update({ email, updated_by: userId });
user.update({ email, updated_by: req.user.id });
}

if (profile_id) {
Expand All @@ -384,9 +406,9 @@ async function updateUser(req, res) {
},
});

if (!profile) return res.status(400).send("Bad Request!");
if (!profile) return res.status(400).send("Profile not found");

user.update({ profile_id, updated_by: userId });
user.update({ profile_id, updated_by: req.user.id });
}

if (role_id) {
Expand All @@ -396,9 +418,9 @@ async function updateUser(req, res) {
},
});

if (!role) return res.status(400).send("Bad Request!");
if (!role) return res.status(400).send("Role not found!");

user.update({ role_id, updated_by: userId });
user.update({ role_id, updated_by: req.user.id });
}

{
Expand Down
11 changes: 11 additions & 0 deletions src/modules/platform/user/user.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ const User = sequelize.define("users", {
},
password_updated_at: {
type: DataTypes.DATE
},
created_by: {
allowNull: true,
type: DataTypes.UUID
},
updated_by: {
allowNull: true,
type: DataTypes.UUID
}
}, {
tableName: 'users',
Expand All @@ -70,6 +78,9 @@ User.prototype.validPassword = function (password) {
return bcrypt.compareSync(password, this.password);
};

User.belongsTo(User, { as: 'createdByUser', foreignKey: 'created_by' });
User.belongsTo(User, { as: 'updatedByUser', foreignKey: 'created_by' });

Profile.hasMany(User, { as: 'users', foreignKey: 'profile_id' });
User.belongsTo(Profile, { as: 'profile', foreignKey: 'profile_id' });

Expand Down

0 comments on commit 7e31156

Please sign in to comment.