|
| 1 | +const { SCHEMA_TYPE } = require("../../Config/schemaType"); |
| 2 | +const { MongoDbCrudOpration } = require("../../utils/mongo-handler/mongoQueries"); |
| 3 | +const mongoose = require("mongoose"); |
| 4 | +const logger = require("../../Config/loggerConfig"); |
| 5 | +const { |
| 6 | + MAX_NOTES_PER_USER, |
| 7 | + isObjectId, |
| 8 | + validateStickyPayload, |
| 9 | + validateReorder, |
| 10 | +} = require("./helpers/stickyRules"); |
| 11 | + |
| 12 | +// Personal sticky notes — one document per note, scoped to the company |
| 13 | +// database AND the owning userId. Every query filters on userId, so a user |
| 14 | +// can only ever read or mutate their own notes. Content is stored as plain |
| 15 | +// text and rendered as text on the client (no HTML). |
| 16 | + |
| 17 | +// Resolve the acting user from the various shapes the clients send. |
| 18 | +const getUserId = (req) => { |
| 19 | + const fromBody = req.body && (req.body.userId || (req.body.userData && (req.body.userData.id || req.body.userData._id))); |
| 20 | + const fromQuery = req.query && req.query.uid; |
| 21 | + return String(fromBody || fromQuery || ''); |
| 22 | +}; |
| 23 | + |
| 24 | +/* GET /api/v2/stickies?uid=<userId> — the user's notes, pinned first. */ |
| 25 | +exports.listStickies = async (req, res) => { |
| 26 | + try { |
| 27 | + const companyId = req.headers['companyid'] || ''; |
| 28 | + const userId = getUserId(req); |
| 29 | + if (!companyId || !userId) { |
| 30 | + return res.send({ status: false, statusText: 'companyId and uid are required.' }); |
| 31 | + } |
| 32 | + |
| 33 | + const stickies = await MongoDbCrudOpration(companyId, { |
| 34 | + type: SCHEMA_TYPE.STICKIES, |
| 35 | + data: [{ userId }, null, { sort: { isPinned: -1, sortIndex: 1, updatedAt: -1 } }], |
| 36 | + }, 'find'); |
| 37 | + |
| 38 | + return res.send({ status: true, statusText: 'Stickies fetched.', data: stickies || [] }); |
| 39 | + } catch (error) { |
| 40 | + logger.error(`ERROR in list stickies: ${error.message}`); |
| 41 | + return res.send({ status: false, statusText: error.message }); |
| 42 | + } |
| 43 | +}; |
| 44 | + |
| 45 | +/* POST /api/v2/stickies body: { content?, color?, userData } */ |
| 46 | +exports.createSticky = async (req, res) => { |
| 47 | + try { |
| 48 | + const companyId = req.headers['companyid'] || ''; |
| 49 | + const userId = getUserId(req); |
| 50 | + if (!companyId || !userId) { |
| 51 | + return res.send({ status: false, statusText: 'companyId and userId are required.' }); |
| 52 | + } |
| 53 | + |
| 54 | + const check = validateStickyPayload(req.body || {}, { partial: false }); |
| 55 | + if (!check.valid) { |
| 56 | + return res.send({ status: false, statusText: check.reason }); |
| 57 | + } |
| 58 | + |
| 59 | + // Per-user count cap (abuse guard). |
| 60 | + const count = await MongoDbCrudOpration(companyId, { |
| 61 | + type: SCHEMA_TYPE.STICKIES, |
| 62 | + data: [{ userId }], |
| 63 | + }, 'countDocuments'); |
| 64 | + if (count >= MAX_NOTES_PER_USER) { |
| 65 | + return res.send({ status: false, statusText: `You can keep at most ${MAX_NOTES_PER_USER} sticky notes.` }); |
| 66 | + } |
| 67 | + |
| 68 | + const created = await MongoDbCrudOpration(companyId, { |
| 69 | + type: SCHEMA_TYPE.STICKIES, |
| 70 | + data: { |
| 71 | + userId, |
| 72 | + title: check.data.title, |
| 73 | + content: check.data.content, |
| 74 | + color: check.data.color, |
| 75 | + sortIndex: 0, |
| 76 | + isPinned: false, |
| 77 | + }, |
| 78 | + }, 'save'); |
| 79 | + |
| 80 | + return res.send({ status: true, statusText: 'Sticky created.', data: created }); |
| 81 | + } catch (error) { |
| 82 | + logger.error(`ERROR in create sticky: ${error.message}`); |
| 83 | + return res.send({ status: false, statusText: error.message }); |
| 84 | + } |
| 85 | +}; |
| 86 | + |
| 87 | +/* PUT /api/v2/stickies/:id body: { content?, color?, isPinned?, userData } */ |
| 88 | +exports.updateSticky = async (req, res) => { |
| 89 | + try { |
| 90 | + const companyId = req.headers['companyid'] || ''; |
| 91 | + const userId = getUserId(req); |
| 92 | + const { id } = req.params; |
| 93 | + if (!companyId || !userId || !isObjectId(id)) { |
| 94 | + return res.send({ status: false, statusText: 'companyId, userId and a valid id are required.' }); |
| 95 | + } |
| 96 | + |
| 97 | + const check = validateStickyPayload(req.body || {}, { partial: true }); |
| 98 | + if (!check.valid) { |
| 99 | + return res.send({ status: false, statusText: check.reason }); |
| 100 | + } |
| 101 | + |
| 102 | + const updated = await MongoDbCrudOpration(companyId, { |
| 103 | + type: SCHEMA_TYPE.STICKIES, |
| 104 | + data: [ |
| 105 | + { _id: new mongoose.Types.ObjectId(id), userId }, |
| 106 | + { $set: check.data }, |
| 107 | + { returnDocument: 'after' }, |
| 108 | + ], |
| 109 | + }, 'findOneAndUpdate'); |
| 110 | + |
| 111 | + if (!updated) { |
| 112 | + return res.send({ status: false, statusText: 'Sticky not found.' }); |
| 113 | + } |
| 114 | + return res.send({ status: true, statusText: 'Sticky updated.', data: updated }); |
| 115 | + } catch (error) { |
| 116 | + logger.error(`ERROR in update sticky: ${error.message}`); |
| 117 | + return res.send({ status: false, statusText: error.message }); |
| 118 | + } |
| 119 | +}; |
| 120 | + |
| 121 | +/* DELETE /api/v2/stickies/:id?uid=<userId> */ |
| 122 | +exports.deleteSticky = async (req, res) => { |
| 123 | + try { |
| 124 | + const companyId = req.headers['companyid'] || ''; |
| 125 | + const userId = getUserId(req); |
| 126 | + const { id } = req.params; |
| 127 | + if (!companyId || !userId || !isObjectId(id)) { |
| 128 | + return res.send({ status: false, statusText: 'companyId, userId and a valid id are required.' }); |
| 129 | + } |
| 130 | + |
| 131 | + const deleted = await MongoDbCrudOpration(companyId, { |
| 132 | + type: SCHEMA_TYPE.STICKIES, |
| 133 | + data: [{ _id: new mongoose.Types.ObjectId(id), userId }], |
| 134 | + }, 'findOneAndDelete'); |
| 135 | + |
| 136 | + if (!deleted) { |
| 137 | + return res.send({ status: false, statusText: 'Sticky not found.' }); |
| 138 | + } |
| 139 | + return res.send({ status: true, statusText: 'Sticky deleted.', data: { _id: id } }); |
| 140 | + } catch (error) { |
| 141 | + logger.error(`ERROR in delete sticky: ${error.message}`); |
| 142 | + return res.send({ status: false, statusText: error.message }); |
| 143 | + } |
| 144 | +}; |
| 145 | + |
| 146 | +/* PUT /api/v2/stickies/reorder body: { ids: [..], userData } */ |
| 147 | +exports.reorderStickies = async (req, res) => { |
| 148 | + try { |
| 149 | + const companyId = req.headers['companyid'] || ''; |
| 150 | + const userId = getUserId(req); |
| 151 | + if (!companyId || !userId) { |
| 152 | + return res.send({ status: false, statusText: 'companyId and userId are required.' }); |
| 153 | + } |
| 154 | + |
| 155 | + const check = validateReorder(req.body && req.body.ids); |
| 156 | + if (!check.valid) { |
| 157 | + return res.send({ status: false, statusText: check.reason }); |
| 158 | + } |
| 159 | + |
| 160 | + // Each note is updated only when it belongs to the user (userId in |
| 161 | + // the filter), so a forged id for someone else's note is a no-op. |
| 162 | + for (let index = 0; index < check.ids.length; index += 1) { |
| 163 | + await MongoDbCrudOpration(companyId, { |
| 164 | + type: SCHEMA_TYPE.STICKIES, |
| 165 | + data: [ |
| 166 | + { _id: new mongoose.Types.ObjectId(check.ids[index]), userId }, |
| 167 | + { $set: { sortIndex: index } }, |
| 168 | + ], |
| 169 | + }, 'updateOne'); |
| 170 | + } |
| 171 | + |
| 172 | + return res.send({ status: true, statusText: 'Stickies reordered.' }); |
| 173 | + } catch (error) { |
| 174 | + logger.error(`ERROR in reorder stickies: ${error.message}`); |
| 175 | + return res.send({ status: false, statusText: error.message }); |
| 176 | + } |
| 177 | +}; |
0 commit comments