Use lean in more places

This commit is contained in:
Phillip Thelen
2026-01-28 20:56:29 +01:00
parent e1a68cd02a
commit b97dfdfa83
12 changed files with 20 additions and 14 deletions
+1 -1
View File
@@ -24,7 +24,7 @@ const api = {};
api.getInboxMessages = {
method: 'GET',
url: '/inbox/messages',
middlewares: [authWithHeaders({ userFieldsToInclude: ['profile', 'contributor', 'backer', 'inbox'] })],
middlewares: [authWithHeaders({ leanUser: true, userFieldsToInclude: ['profile', 'contributor', 'backer', 'inbox'] })],
async handler (req, res) {
const { user } = res.locals;
const { page } = req.query;
+2 -2
View File
@@ -388,7 +388,7 @@ api.getUserTasks = {
method: 'GET',
url: '/tasks/user',
middlewares: [authWithHeaders({
// Some fields (including _id, preferences) are always loaded (see middlewares/auth)
leanUser: true,
userFieldsToInclude: ['tasksOrder'],
})],
async handler (req, res) {
@@ -953,7 +953,7 @@ api.addChecklistItem = {
api.scoreCheckListItem = {
method: 'POST',
url: '/tasks/:taskId/checklist/:itemId/score',
middlewares: [authWithHeaders()],
middlewares: [authWithHeaders({ leanUser: true, userFieldsToInclude: ['_id'] })],
async handler (req, res) {
const { user } = res.locals;
+1 -1
View File
@@ -406,7 +406,7 @@ api.getUserAnonymized = {
{ type: { $in: ['habit', 'daily', 'reward'] } },
],
};
const tasks = await Tasks.Task.find(query).exec();
const tasks = await Tasks.Task.find(query).lean().exec();
forEach(tasks, task => {
task.text = 'task text';
@@ -22,6 +22,7 @@ api.purchaseHistory = {
let transactions = await Transaction
.find({ userId: req.params.memberId })
.sort({ createdAt: -1 })
.lean()
.exec();
if (!res.locals.user.hasPermission('userSupport')) {
+3 -1
View File
@@ -313,7 +313,9 @@ api.purchaseHistory = {
url: '/user/purchase-history',
async handler (req, res) {
const { user } = res.locals;
const transactions = await Transaction.find({ userId: user._id }).sort({ createdAt: -1 });
const transactions = await Transaction.find({ userId: user._id })
.sort({ createdAt: -1 })
.lean();
res.respond(200, transactions);
},
};
@@ -53,7 +53,7 @@ api.exportUserHistory = {
const tasks = await Tasks.Task.find({
userId: user._id,
type: { $in: ['habit', 'daily'] },
}).exec();
}).lean().exec();
const output = [
['Task Name', 'Task ID', 'Task Type', 'Date', 'Value'],
@@ -92,7 +92,7 @@ async function _getUserDataForExport (user) {
const [tasks, messages] = await Promise.all([
Tasks.Task.find({
userId: user._id,
}).exec(),
}).lean().exec(),
inboxLib.getUserInbox(user, { asArray: false }),
]);
@@ -100,7 +100,6 @@ async function _getUserDataForExport (user) {
userData.inbox.messages = messages;
_(tasks)
.map(task => task.toJSON())
.groupBy(task => task.type)
.forEach((tasksPerType, taskType) => {
userData.tasks[`${taskType}s`] = tasksPerType;
+1
View File
@@ -22,6 +22,7 @@ export async function sendChatPushNotifications (user, group, message, mentions,
'party._id': group._id,
_id: { $ne: user._id },
})
.lean()
.select('preferences.pushNotifications preferences.language profile.name pushDevices auth.local.username')
.exec();
+2 -2
View File
@@ -25,13 +25,13 @@ export async function getGroupChat (group, options = {}) {
.sort('-timestamp');
if (before) {
const beforeMessage = await Chat.findOne({ _id: before }).exec();
const beforeMessage = await Chat.findOne({ _id: before }, { timestamp: 1 }).lean().exec();
if (beforeMessage) {
query = query.where('timestamp').lt(beforeMessage.timestamp);
}
}
const groupChat = await query.limit(effectiveLimit).exec();
const groupChat = await query.limit(effectiveLimit).lean().exec();
// @TODO: Concat old chat to keep continuity of chat stored on group object
const currentGroupChat = group.chat || [];
@@ -22,7 +22,7 @@ async function usersMapByConversations (users) {
stats: 1,
flags: 1,
inbox: 1,
}).exec();
}).lean().exec();
for (const usr of loadedUsers) {
const loadedUserConversation = {
+4 -1
View File
@@ -169,7 +169,10 @@ api.subscribe = async function subscribe (user, receipt, headers, nextPaymentPro
{ 'purchased.plan.customerId': purchase.originalTransactionId },
{ 'purchased.plan.customerId': purchase.transactionId },
],
}).exec();
}, {
_id: 1,
'purchased.plan': 1,
}).lean().exec();
if (existingUsers.length > 0) {
if (purchase.originalTransactionId === purchase.transactionId) {
throw new NotAuthorized(this.constants.RESPONSE_ALREADY_USED);
+1 -1
View File
@@ -178,7 +178,7 @@ async function getTasks (req, res, options = {}) {
],
},
{ _id: 1 },
).exec();
).lean().exec();
}
if (upgradedGroups.length > 0) {
for (const upgradedGroup of upgradedGroups) {
+1 -1
View File
@@ -186,7 +186,7 @@ export async function update (req, res, { isV3 = false }) {
],
}, {
_id: 1,
}).exec();
}).lean().exec();
matchingGroupsArray = _.map(matchingGroups, groupRecord => groupRecord._id);
}