log armoire, quoest response and cron events to history

This commit is contained in:
Phillip Thelen
2024-08-22 15:20:02 +02:00
parent 62f5b9698a
commit 38cad7102f
7 changed files with 157 additions and 7 deletions
+13 -6
View File
@@ -15,6 +15,9 @@ import {
import {
model as NewsPost,
} from '../newsPost';
import {
model as UserHistory,
} from '../userHistory';
import { // eslint-disable-line import/no-cycle
userActivityWebhook,
} from '../../libs/webhook';
@@ -237,7 +240,7 @@ schema.pre('validate', function preValidateUser (next) {
next();
});
schema.pre('save', true, function preSaveUser (next, done) {
schema.pre('save', true, async function preSaveUser (next, done) {
next();
// VERY IMPORTANT NOTE: when only some fields from an user document are selected
@@ -360,6 +363,13 @@ schema.pre('save', true, function preSaveUser (next, done) {
// Unset the field so this is run only once
this.flags.lastWeeklyRecapDiscriminator = undefined;
}
if (!this.flags.initializedUserHistory) {
this.flags.initializedUserHistory = true;
const history = UserHistory();
history.userId = this._id;
await history.save();
console.log('Initialized user history');
}
}
// Enforce min/max values without displaying schema errors to end user
@@ -396,12 +406,9 @@ schema.pre('save', true, function preSaveUser (next, done) {
// Populate new users with default content
if (this.isNew) {
_setUpNewUser(this)
.then(() => done())
.catch(done);
} else {
done();
await _setUpNewUser(this);
}
done();
});
schema.pre('updateOne', function preUpdateUser () {
+1
View File
@@ -313,6 +313,7 @@ export const UserSchema = new Schema({
warnedLowHealth: { $type: Boolean, default: false },
verifiedUsername: { $type: Boolean, default: false },
thirdPartyTools: { $type: Date },
initializedUserHistory: { $type: Boolean, default: false },
},
history: {
+117
View File
@@ -0,0 +1,117 @@
import mongoose from 'mongoose';
import validator from 'validator';
import baseModel from '../libs/baseModel';
const { Schema } = mongoose;
export const schema = new Schema({
userId: {
$type: String,
ref: 'User',
required: true,
validate: [v => validator.isUUID(v), 'Invalid uuid for userhistory.'],
index: true,
unique: true,
},
armoire: [
{
_id: false,
timestamp: { $type: Date, required: true },
reward: { $type: String, required: true },
},
],
questInviteResponses: [
{
_id: false,
timestamp: { $type: Date, required: true },
quest: { $type: String, required: true },
response: { $type: String, required: true },
},
],
cron: [
{
_id: false,
timestamp: { $type: Date, required: true },
},
],
}, {
strict: true,
minimize: false, // So empty objects are returned
typeKey: '$type', // So that we can use fields named `type`
});
schema.plugin(baseModel, {
noSet: ['id', '_id', 'userId'],
timestamps: true,
_id: false, // using custom _id
});
export const model = mongoose.model('UserHistory', schema);
const commitUserHistoryUpdate = function commitUserHistoryUpdate (update) {
const data = {
$push: {
},
};
if (update.data.armoire.length) {
data.$push.armoire = {
$each: update.data.armoire,
$sort: { timestamp: -1 },
$slice: 10,
};
}
if (update.data.questInviteResponses.length) {
data.$push.questInviteResponses = {
$each: update.data.questInviteResponses,
$sort: { timestamp: -1 },
$slice: 10,
};
}
if (update.data.cron.length > 0) {
data.$push.cron = {
$each: update.data.cron,
$sort: { timestamp: -1 },
$slice: 10,
};
}
return model.updateOne(
{ userId: update.userId },
data,
).exec();
};
model.beginUserHistoryUpdate = function beginUserHistoryUpdate (userID) {
return {
userId: userID,
data: {
armoire: [],
questInviteResponses: [],
cron: [],
},
withArmoire: function withArmoire (reward) {
this.data.armoire.push({
timestamp: new Date(),
reward,
});
return this;
},
withQuestInviteResponse: function withQuestInviteResponse (quest, response) {
this.data.questInviteResponses.push({
timestamp: new Date(),
quest,
response,
});
return this;
},
withCron: function withCron () {
this.data.cron.push({
timestamp: new Date(),
});
return this;
},
commit: function commit () {
commitUserHistoryUpdate(this);
},
};
};