Skip to content

Commit

Permalink
Merge pull request #196 from aitok-ai/jinshi/quota-status
Browse files Browse the repository at this point in the history
feat: Add monthlyQuotaConsumed field to GET /api/user API
  • Loading branch information
jinzishuai authored May 15, 2024
2 parents 1f9931a + 96d6e1c commit 8e345dc
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
16 changes: 14 additions & 2 deletions api/server/controllers/UserController.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
const { updateUserPluginsService } = require('../services/UserService');
const { updateUserPluginAuth, deleteUserPluginAuth } = require('../services/PluginService');
const { getUserMessageQuotaUsagePastDays } = require('../middleware/messageQuota');
const User = require('../../models/User');

const getUserController = async (req, res) => {
try {
const { userId } = req.params;
if (userId == undefined || userId === req.user.id) {res.status(200).send(req.user);}
else {
if (userId === undefined || userId === req.user.id) {
// information about the current user
const monthlyQuotaConsumed = await getUserMessageQuotaUsagePastDays(req.user, 30); // This value might be dynamic based on your application logic

// Extend req.user with the new field
const response = {
...req.user.toJSON(),
monthlyQuotaConsumed: monthlyQuotaConsumed,
};
res.status(200).send(response);
} else {
// information about another user, without even authentification.
// TODO: this might be a security issue
const user = await User.findById(userId).exec();
const id = user._id;
const name = user.name;
Expand Down
2 changes: 2 additions & 0 deletions api/server/middleware/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const concurrentLimiter = require('./concurrentLimiter');
const validateMessageReq = require('./validateMessageReq');
const buildEndpointOption = require('./buildEndpointOption');
const validateRegistration = require('./validateRegistration');
const messageQuota = require('./messageQuota');

module.exports = {
...abortMiddleware,
Expand All @@ -28,4 +29,5 @@ module.exports = {
validateMessageReq,
buildEndpointOption,
validateRegistration,
messageQuota,
};
34 changes: 34 additions & 0 deletions api/server/middleware/messageQuota.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const { getMessagesCount } = require('../../models');

const getUserMessageQuotaUsagePastDays = async (user, days = 30) => {
let currentTime = new Date();
let quota = 0;
if ('proMemberExpiredAt' in user && user.proMemberExpiredAt > currentTime) {
// If not proMember, check quota
quota = JSON.parse(process.env['CHAT_QUOTA_PER_MONTH_PRO_MEMBER']);
} else {
quota = JSON.parse(process.env['CHAT_QUOTA_PER_MONTH']);
}

let someTimeAgo = currentTime;
someTimeAgo.setSeconds(currentTime.getSeconds() - 60 * 60 * 24 * days); // 30 days

let quotaUsage = {};

let promises = Object.keys(quota).map(async (model) => {
let messagesCount = await getMessagesCount({
$and: [{ senderId: user.id }, { model: model }, { updatedAt: { $gte: someTimeAgo } }],
});
quotaUsage[model] = {
consumed: messagesCount,
quota: quota[model],
};
console.log(model, quotaUsage[model]);
});
await Promise.all(promises);
return quotaUsage;
};

module.exports = {
getUserMessageQuotaUsagePastDays,
};

0 comments on commit 8e345dc

Please sign in to comment.