Skip to content

Commit

Permalink
Merge branch 'staging' into question-service_categories
Browse files Browse the repository at this point in the history
  • Loading branch information
pzl111 authored Oct 29, 2024
2 parents 4301844 + a63ce43 commit b42ed17
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
8 changes: 4 additions & 4 deletions user-service/controllers/user-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export async function loginUser(req, res) {
return res.status(400).json({ statusCode: 400, message: "Email and password are required" })
}

// Check if user exists
// Check if user exists (isDeleted is false)
const user = await ormFindUser(email);
console.log(user)
if (!user) {
Expand Down Expand Up @@ -63,7 +63,7 @@ export async function createUser(req, res) {
return res.status(400).json({ statusCode: 400, message: "Email, password and displayName are required" })
}

// Check if user already exists
// Check if user already exists (isDeleted is false)
const existingUser = await ormFindUser(email);
console.log(existingUser)
if (existingUser) {
Expand Down Expand Up @@ -96,7 +96,7 @@ export async function createUser(req, res) {
}
}

export async function deleteUser(req, res) {
export async function deleteUser(req, res) { // SOFT DELETE
try {
const email = req.user.email; // Delete the user specified from token

Expand All @@ -112,7 +112,7 @@ export async function deleteUser(req, res) {
return res.status(404).json({ statusCode: 404, message: "User not found" })
}

// Delete user
// Delete user (soft delete, only sets isDeleted to true)
const user = await ormDeleteUser(email);
if (!user) {
return res.status(500).json({ statusCode: 500, message: "Unknown server error" })
Expand Down
6 changes: 3 additions & 3 deletions user-service/models/repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ export async function _createUser(param) {
}

export async function _deleteUser(param) {
return UserModel.findOneAndDelete({ email: param });
return UserModel.findOneAndUpdate({ email: param }, { isDeleted: true }, { new: true }); // soft delete
}

export async function _findUser(param) {
return UserModel.findOne({ email: param });
return UserModel.findOne({ email: param, isDeleted: false });
}

export async function _updateUser({ email, prop }) {
return UserModel.findOneAndUpdate({ email: email }, prop, { new: true });
return UserModel.findOneAndUpdate({ email: email, isDeleted: false }, prop, { new: true });
}
5 changes: 5 additions & 0 deletions user-service/models/user-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ const userSchema = mongoose.Schema(
type: Boolean,
required: true,
default: false,
},
isDeleted: {
type: Boolean,
required: true,
default: false,
}
},
{
Expand Down

0 comments on commit b42ed17

Please sign in to comment.