shuttlepro-shared 1.2.8 → 1.2.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -92,153 +92,80 @@ const {
92
92
  deleteRedisData,
93
93
  } = require("../../config/redis");
94
94
 
95
- const createConfig = (env) => ({
96
- cache: {
97
- keys: {
98
- ALL_LABELS: "labels:all",
99
- WORKSPACE_LABELS: R.memoizeWith(
100
- R.identity,
101
- (workspaceId) => `labels:workspace:${workspaceId}`
102
- ),
103
- LABEL_DETAILS: R.memoizeWith(
104
- R.identity,
105
- (labelId) => `labels:details:${labelId}`
106
- ),
107
- },
108
- expiry: 60 * 60,
109
- },
110
- });
111
-
112
- const createLabelRepository = (dependencies) => {
113
- const { Label, redisConfig } = dependencies;
114
-
115
- const generateCacheKey = R.pipe(
116
- R.toPairs,
117
- R.map(([key, value]) => `${key}:${JSON.stringify(value)}`),
118
- R.join("|"),
119
- R.concat("labels:query:")
120
- );
95
+ const CACHE_KEYS = {
96
+ ALL_LABELS: "labels:all",
97
+ WORKSPACE_LABELS: (workspaceId) => `labels:workspace:${workspaceId}`,
98
+ LABEL_DETAILS: (labelId) => `labels:details:${labelId}`,
99
+ };
121
100
 
122
- const withCache = R.curry(async (cacheKey, fetchFn, forceFresh = false) => {
123
- if (!forceFresh) {
124
- const cachedData = await getRedisData(cacheKey);
125
- if (cachedData) return cachedData;
126
- }
101
+ const CACHE_EXPIRY = 60 * 60; // 1 hour
127
102
 
128
- const data = await fetchFn();
103
+ // Utility function for caching
104
+ const withCache = async (cacheKey, fetchFn, forceFresh = false) => {
105
+ if (!forceFresh) {
106
+ const cachedData = await getRedisData(cacheKey);
107
+ if (cachedData) return cachedData;
108
+ }
129
109
 
130
- if (data && data.length) {
131
- await setRedisData(cacheKey, data, null, redisConfig.cache.expiry);
132
- }
110
+ const data = await fetchFn();
111
+ if (data && data.length) {
112
+ await setRedisData(cacheKey, data, null, CACHE_EXPIRY);
113
+ }
133
114
 
134
- return data;
135
- });
115
+ return data;
116
+ };
136
117
 
137
- const validateLabel = R.curry((existingLabels, newLabel) => {
138
- const isDuplicate = R.any(
139
- (label) =>
140
- label.title.toLowerCase() === newLabel.title.toLowerCase() &&
141
- R.equals(label.workspaceId, newLabel.workspaceId)
142
- )(existingLabels);
118
+ // Core functions
119
+ const createLabel = async (labelData) => {
120
+ const existingLabels = await Label.find();
121
+ const isDuplicate = existingLabels.some(
122
+ (label) =>
123
+ label.title.toLowerCase() === labelData.title.toLowerCase() &&
124
+ label.workspaceId === labelData.workspaceId
125
+ );
143
126
 
144
- if (isDuplicate) {
145
- throw new Error("Label with this title already exists");
146
- }
127
+ if (isDuplicate) throw new Error("Label with this title already exists");
147
128
 
148
- return newLabel;
149
- });
129
+ const newLabel = new Label(labelData);
130
+ return await newLabel.save();
131
+ };
150
132
 
151
- const createLabel = async (labelData) => {
152
- const create = R.pipe(
153
- (label) => label.save(),
154
- validateLabel(await Label.find()),
155
- Label.hydrate
156
- );
157
-
158
- return create(labelData);
159
- };
160
-
161
- const findLabels = R.curry(async (options = {}, query = {}) => {
162
- const {
163
- lean = true,
164
- populate = [],
165
- sort = { createdAt: -1 },
166
- limit = 100,
167
- } = options;
168
-
169
- const executeQuery = R.pipe(
170
- R.when(R.always(lean), R.map(R.prop("toObject"))),
171
- (labels) => (populate.length ? Label.populate(labels, populate) : labels),
172
- (labels) => R.take(limit, labels),
173
- (labels) =>
174
- R.sort((a, b) => new Date(b.createdAt) - new Date(a.createdAt), labels)
175
- );
176
-
177
- const cachedFind = await withCache(
178
- generateCacheKey(query),
179
- () => Label.find(query).exec(),
180
- false
181
- );
182
-
183
- return executeQuery(cachedFind);
184
- });
133
+ const findLabels = async (query = {}, options = {}) => {
134
+ return await withCache(JSON.stringify(query), () =>
135
+ Label.find(query)
136
+ .sort({ createdAt: -1 })
137
+ .limit(options.limit || 100)
138
+ .exec()
139
+ );
140
+ };
185
141
 
186
- const findAllLabels = R.curry(async (query = {}) => {
187
- return await withCache(
188
- generateCacheKey(query),
189
- () => Label.find(query).exec(),
190
- false
191
- );
192
- });
142
+ const findAllLabels = async (query = {}) => {
143
+ return await withCache(JSON.stringify(query), () => Label.find(query).exec());
144
+ };
193
145
 
194
- const updateLabel = R.curry(async (labelId, updateData) => {
195
- const update = R.pipe(
196
- R.tap(async (updatedLabel) => {
197
- await setRedisData(
198
- redisConfig.cache.keys.LABEL_DETAILS(labelId),
199
- updatedLabel.toObject()
200
- );
201
- }),
202
- (label) => label.save(),
203
- (label) => {
204
- const updatedFields = R.mergeDeepRight(label.toObject(), updateData);
205
- return Label.hydrate(updatedFields);
206
- },
207
- () => Label.findById(labelId)
208
- );
209
-
210
- return update();
146
+ const updateLabel = async (labelId, updateData) => {
147
+ const updatedLabel = await Label.findByIdAndUpdate(labelId, updateData, {
148
+ new: true,
211
149
  });
212
-
213
- const deleteLabel = async (labelId) => {
214
- const performDelete = R.pipe(
215
- R.tap(async () => {
216
- await deleteRedisData(redisConfig.cache.keys.LABEL_DETAILS(labelId));
217
- }),
218
- (label) => label.remove(),
219
- () => Label.findById(labelId)
220
- );
221
-
222
- return performDelete();
223
- };
224
-
225
- const filterLabels = R.curry((filterFn, labels) =>
226
- R.filter(filterFn, labels)
150
+ await setRedisData(
151
+ CACHE_KEYS.LABEL_DETAILS(labelId),
152
+ updatedLabel.toObject(),
153
+ null,
154
+ CACHE_EXPIRY
227
155
  );
156
+ return updatedLabel;
157
+ };
228
158
 
229
- return {
230
- createLabel,
231
- findLabels,
232
- findAllLabels,
233
- updateLabel,
234
- deleteLabel,
235
- filterLabels,
236
- };
159
+ const deleteLabel = async (labelId) => {
160
+ await deleteRedisData(CACHE_KEYS.LABEL_DETAILS(labelId));
161
+ return await Label.findByIdAndDelete(labelId);
237
162
  };
238
163
 
239
- const dependencies = {
240
- Label,
241
- redisConfig: createConfig(process.env),
164
+ module.exports = {
165
+ createLabel,
166
+ findLabels,
167
+ findAllLabels,
168
+ updateLabel,
169
+ deleteLabel,
242
170
  };
243
171
 
244
- module.exports = createLabelRepository(dependencies);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "shuttlepro-shared",
3
- "version": "1.2.8",
3
+ "version": "1.2.9",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {