0

I'm building an application with express framework and I want to set a default subscription period to user when get registered ( 6 months by default for example ), after that period, user must pay a specific amount to get access to the app, can some one explain or help by technicals ideas ?

const mongoose = require('mongoose');    
const schema = new mongoose.Schema({
  email: {
    type: String,
    unique: true,
    required: true
  },
  passwordHash: {
    type: String,
    required: true
  },
  title: {
    type: String,
    required: true
  },
  firstName: {
    type: String,
    required: true
  },
  lastName: {
    type: String,
    required: true
  },
  phone: {
    type: String,
    required: true
  },
  acceptTerms: Boolean,

  subscriptionPeriod:{
    type: Number,
    default: 6
  },
  payed:{
    type:Boolean,
    default: true
  },
  role: {
    type: String,
    required: true
  },
  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);
547n00n
  • 1,356
  • 6
  • 30
  • 55

1 Answers1

0
  1. You can use scheduler in node js which will invoke like whatever duration you want to set. There is a package node-cron node-cron package. With this you can scheduler every day and write logic to check which user plan has expired, based on that you can trigger email or disable the account.
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 lambda can also be used if working with aws. For more information visit this link Scheduling with aws lambda