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
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
131
|
-
|
|
132
|
-
|
|
110
|
+
const data = await fetchFn();
|
|
111
|
+
if (data && data.length) {
|
|
112
|
+
await setRedisData(cacheKey, data, null, CACHE_EXPIRY);
|
|
113
|
+
}
|
|
133
114
|
|
|
134
|
-
|
|
135
|
-
|
|
115
|
+
return data;
|
|
116
|
+
};
|
|
136
117
|
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
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
|
-
|
|
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
|
-
|
|
149
|
-
|
|
129
|
+
const newLabel = new Label(labelData);
|
|
130
|
+
return await newLabel.save();
|
|
131
|
+
};
|
|
150
132
|
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
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
|
-
|
|
187
|
-
|
|
188
|
-
|
|
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
|
-
|
|
195
|
-
|
|
196
|
-
|
|
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
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
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
|
-
|
|
230
|
-
|
|
231
|
-
|
|
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
|
-
|
|
240
|
-
|
|
241
|
-
|
|
164
|
+
module.exports = {
|
|
165
|
+
createLabel,
|
|
166
|
+
findLabels,
|
|
167
|
+
findAllLabels,
|
|
168
|
+
updateLabel,
|
|
169
|
+
deleteLabel,
|
|
242
170
|
};
|
|
243
171
|
|
|
244
|
-
module.exports = createLabelRepository(dependencies);
|