微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

NodeJS / Express:为用户帐户添加默认订阅期

如何解决NodeJS / Express:为用户帐户添加默认订阅期

我正在构建一个带有 express 框架的应用程序,我想在注册时为用户设置一个订阅期(例如认为 6 个月),在该期限之后,用户必须支付特定金额才能访问应用程序,有人可以通过技术思路解释或帮助吗?

const mongoose = require('mongoose');    
const schema = new mongoose.Schema({
  email: {
    type: String,unique: true,required: true
  },passwordHash: {
    type: String,title: {
    type: String,firstName: {
    type: String,lastName: {
    type: String,phone: {
    type: String,acceptTerms: Boolean,subscriptionPeriod:{
    type: Number,default: 6
  },payed:{
    type:Boolean,default: true
  },role: {
    type: String,verificationToken: String,verified: Date,resetToken: {
    token: String,expires: Date
  },passwordReset: Date,created: {
    type: Date,default: Date.Now
  },updated: Date
});

schema.virtual('isverified').get(function() {
  return !!(this.verified || this.passwordReset);
});    
module.exports = mongoose.model('User',schema);

解决方法

  1. 您可以在 node js 中使用调度程序,它会像您想要设置的任何持续时间一样调用。有一个包 node-cron node-cron package。 有了这个,您可以每天安排时间并编写逻辑来检查哪个用户计划已过期,根据此您可以触发电子邮件或禁用帐户。
var cron = require('node-cron');
var job = cron.schedule('0 0 0 * * *',function() {
 //will run every day at 12:00 AM
  //set logic here to check if expiry date is 6 month
})
  1. 如果使用 aws,也可以使用 AWS lambda。有关更多信息,请访问此链接 Scheduling with aws lambda

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。