const User = require('../models/User'); const ERROR_MESSAGES = require('../utils/errorMessages'); <% if (communication !== 'GraphQL') { -%> const HTTP_STATUS = require('../utils/httpCodes'); <% } -%> const logger = require('../utils/logger'); <%_ if (caching === 'Redis') { -%> const cacheService = require('../config/redisClient'); <%_ } else if (caching === 'Memory Cache') { -%> const cacheService = require('../config/memoryCache'); <%_ } -%> <%_ if (communication === 'Kafka') { -%> const { sendMessage } = require('../services/kafkaService'); const { KAFKA_ACTIONS } = require('../utils/kafkaEvents'); <%_ } -%> <% if (auth.includes('JWT')) { %>const bcrypt = require('bcryptjs');<% } %> <% if (communication === 'GraphQL') { -%> const getUsers = async () => { try { <%_ if (caching === 'Redis' || caching === 'Memory Cache') { -%> const users = await cacheService.getOrSet('users:all', async () => { <%_ if (database === 'MongoDB' || database === 'None') { -%> return await User.find(); <%_ } else { -%> return await User.findAll(); <%_ } -%> }, 60); <%_ } else { -%> <%_ if (database === 'MongoDB' || database === 'None') { -%> const users = await User.find(); <%_ } else { -%> const users = await User.findAll(); <%_ } -%> <%_ } -%> return users; } catch (error) { logger.error(`${ERROR_MESSAGES.FETCH_USERS_ERROR}:`, error); throw error; } }; const createUser = async (data) => { try { const { name, email<% if (auth.includes('JWT')) { %>, password<% } %> } = data; <% if (auth.includes('JWT')) { %> let user; if (password) { const hashedPassword = await bcrypt.hash(password, 10); user = await User.create({ name, email, password: hashedPassword }); } else { user = await User.create({ name, email }); } <% } else { %> const user = await User.create({ name, email }); <% } %> <%_ if (caching === 'Redis' || caching === 'Memory Cache') { -%> await cacheService.del('users:all'); <%_ } -%> <%_ if (communication === 'Kafka') { -%> await sendMessage('user-topic', JSON.stringify({ action: KAFKA_ACTIONS.USER_CREATED, payload: { id: user.id || user._id, email: user.email } }), (user.id || user._id).toString()); <%_ } -%> const userObj = user.toJSON ? user.toJSON() : { ...user }; delete userObj.password; return userObj; } catch (error) { logger.error(`${ERROR_MESSAGES.CREATE_USER_ERROR}:`, error); throw error; } }; const updateUser = async (id, data) => { try { <%_ if (database === 'MongoDB') { -%> const updatedUser = await User.findByIdAndUpdate(id, data, { new: true }); if (!updatedUser) throw new Error(ERROR_MESSAGES.USER_NOT_FOUND); <%_ } else if (database === 'None') { -%> const user = await User.findByPk(id); if (!user) throw new Error(ERROR_MESSAGES.USER_NOT_FOUND); await User.update(id, data); const updatedUser = await User.findByPk(id); <%_ } else { -%> const user = await User.findByPk(id); if (!user) throw new Error(ERROR_MESSAGES.USER_NOT_FOUND); await user.update(data); const updatedUser = user; <%_ } -%> <%_ if (caching === 'Redis' || caching === 'Memory Cache') { -%> await cacheService.del('users:all'); <%_ } -%> <%_ if (communication === 'Kafka') { -%> await sendMessage('user-topic', JSON.stringify({ action: KAFKA_ACTIONS.USER_UPDATED, payload: { id, email: updatedUser?.email } }), id); <%_ } -%> return updatedUser; } catch (error) { logger.error(`${ERROR_MESSAGES.UPDATE_USER_ERROR}:`, error); throw error; } }; const deleteUser = async (id) => { try { <%_ if (database === 'MongoDB') { -%> const deleted = await User.findByIdAndDelete(id); if (!deleted) throw new Error(ERROR_MESSAGES.USER_NOT_FOUND); <%_ } else if (database === 'None') { -%> const deleted = await User.destroy(id); if (!deleted) throw new Error(ERROR_MESSAGES.USER_NOT_FOUND); <%_ } else { -%> const user = await User.findByPk(id); if (!user) throw new Error(ERROR_MESSAGES.USER_NOT_FOUND); await user.destroy(); <%_ } -%> <%_ if (caching === 'Redis' || caching === 'Memory Cache') { -%> await cacheService.del('users:all'); <%_ } -%> <%_ if (communication === 'Kafka') { -%> await sendMessage('user-topic', JSON.stringify({ action: KAFKA_ACTIONS.USER_DELETED, payload: { id } }), id); <%_ } -%> return true; } catch (error) { logger.error(`${ERROR_MESSAGES.DELETE_USER_ERROR}:`, error); throw error; } }; module.exports = { getUsers, createUser, updateUser, deleteUser }; <% } else { -%> const getUsers = async (req, res, next) => { try { <%_ if (caching === 'Redis' || caching === 'Memory Cache') { -%> const users = await cacheService.getOrSet('users:all', async () => { <%_ if (database === 'MongoDB' || database === 'None') { -%> return await User.find(); <%_ } else { -%> return await User.findAll(); <%_ } -%> }, 60); <%_ } else { -%> <%_ if (database === 'MongoDB' || database === 'None') { -%> const users = await User.find(); <%_ } else { -%> const users = await User.findAll(); <%_ } -%> <%_ } -%> res.json(users); } catch (error) { logger.error(`${ERROR_MESSAGES.FETCH_USERS_ERROR}:`, error); next(error); } }; const createUser = async (req, res, next) => { try { const { name, email, password } = req.body || {}; <% if (auth.includes('JWT')) { %> if (!password) { return res.status(HTTP_STATUS.BAD_REQUEST).json({ error: 'Password is required' }); } const hashedPassword = await bcrypt.hash(password, 10); const user = await User.create({ name, email, password: hashedPassword }); <% } else { %> const user = await User.create({ name, email }); <% } %> <%_ if (caching === 'Redis' || caching === 'Memory Cache') { -%> await cacheService.del('users:all'); <%_ } -%> <%_ if (communication === 'Kafka') { -%> await sendMessage('user-topic', JSON.stringify({ action: KAFKA_ACTIONS.USER_CREATED, payload: { id: user.id || user._id, email: user.email } }), (user.id || user._id).toString()); <%_ } -%> const userObj = user.toJSON ? user.toJSON() : { ...user }; delete userObj.password; res.status(HTTP_STATUS.CREATED).json(userObj); } catch (error) { logger.error(`${ERROR_MESSAGES.CREATE_USER_ERROR}:`, error); next(error); } }; const updateUser = async (req, res, next) => { try { const { id } = req.params; const { name, email } = req.body || {}; <%_ if (database === 'MongoDB') { -%> const updatedUser = await User.findByIdAndUpdate(id, { name, email }, { new: true }); if (!updatedUser) { return res.status(HTTP_STATUS.NOT_FOUND).json({ error: ERROR_MESSAGES.USER_NOT_FOUND }); } <%_ } else if (database === 'None') { -%> const user = await User.findByPk(id); if (!user) { return res.status(HTTP_STATUS.NOT_FOUND).json({ error: ERROR_MESSAGES.USER_NOT_FOUND }); } await User.update(id, { name, email }); const updatedUser = await User.findByPk(id); <%_ } else { -%> const user = await User.findByPk(id); if (!user) { return res.status(HTTP_STATUS.NOT_FOUND).json({ error: ERROR_MESSAGES.USER_NOT_FOUND }); } await user.update({ name, email }); const updatedUser = user; <%_ } -%> <%_ if (caching === 'Redis' || caching === 'Memory Cache') { -%> await cacheService.del('users:all'); <%_ } -%> <%_ if (communication === 'Kafka') { -%> await sendMessage('user-topic', JSON.stringify({ action: KAFKA_ACTIONS.USER_UPDATED, payload: { id, email: updatedUser?.email } }), id); <%_ } -%> res.status(HTTP_STATUS.OK).json(updatedUser); } catch (error) { logger.error(`${ERROR_MESSAGES.UPDATE_USER_ERROR}:`, error); next(error); } }; const deleteUser = async (req, res, next) => { try { const { id } = req.params; <%_ if (database === 'MongoDB') { -%> const deleted = await User.findByIdAndDelete(id); if (!deleted) { return res.status(HTTP_STATUS.NOT_FOUND).json({ error: ERROR_MESSAGES.USER_NOT_FOUND }); } <%_ } else if (database === 'None') { -%> const deleted = await User.destroy(id); if (!deleted) { return res.status(HTTP_STATUS.NOT_FOUND).json({ error: ERROR_MESSAGES.USER_NOT_FOUND }); } <%_ } else { -%> const user = await User.findByPk(id); if (!user) { return res.status(HTTP_STATUS.NOT_FOUND).json({ error: ERROR_MESSAGES.USER_NOT_FOUND }); } await user.destroy(); <%_ } -%> <%_ if (caching === 'Redis' || caching === 'Memory Cache') { -%> await cacheService.del('users:all'); <%_ } -%> <%_ if (communication === 'Kafka') { -%> await sendMessage('user-topic', JSON.stringify({ action: KAFKA_ACTIONS.USER_DELETED, payload: { id } }), id); <%_ } -%> res.status(HTTP_STATUS.OK).json({ message: 'User deleted successfully' }); } catch (error) { logger.error(`${ERROR_MESSAGES.DELETE_USER_ERROR}:`, error); next(error); } }; module.exports = { getUsers, createUser, updateUser, deleteUser, model: User }; <% } -%>