Home > Mobile >  remote scheduling in node js
remote scheduling in node js

Time:10-19

So Im developing an app and my backend uses nodejs, and in this app I need to create a scheduling so I need that the user pass the days of week that he works on, How can I do this ? here is my user model code rigth now:

const mongoose = require('../../database')

const bcrypt = require('bcryptjs')
const nodegeocoder = require('node-geocoder')
const { json } = require('body-parser')
const options ={
    provider: 'openstreetmap',
    //apiKey: 'WH3W5ZphcP0DSqvD22vgR6N_b6FiWnLWHt9mlSGs9NU',
   
}

const geocoder = nodegeocoder(options)


const UserScheme = new mongoose.Schema({
    
    title:{
        type: String,
        required: true,

    },

    description:{
        type: String,
        required: false
    },
    email:{
        type: String,
        unique :true,
        required: true,
        lowercase: true
    },


    contato:{
        type: String,
        required:false,
        select:true
    },
    horarios:{
        type: Date,
        required:false,
        default:Date.arguments , //not sure about this "horarios"
        select: true
    },
 

So what should I use here in the horarios ? for it to create a weekdays loop so he can say in what weekdays he works

})




UserScheme.pre('save', async function(next){
    const hash = await bcrypt.hash(this.password, 10)
    this.password = hash
    
        
   next();     
    
})
 

const user = mongoose.model('User', UserScheme)

module.exports = user

CodePudding user response:

Not entirely sure what your question is, but i suppose you mean how to specify which days he will be working on? In that case, you can maybe create an array, as follows,

"horarios":[
"monday":{startTime: something, endTime: something},
"tuesday":{startTime: something, endTime: something},
...
...
"saturday":{startTime: something, endTime: something},
]

Instead of "monday","tuesday".."sunday" you can have numbers like 0,1,2..6 if thats better for programming purposes.

CodePudding user response:

Ideally a scalable solution to manage this would be to use cron jobs.

Here is how one might go about it.

  1. Create a model to store the job data.
  2. From your frontend where user can choose weekly/monthly etc. save the details in the job data model.
  3. Run a cron job every 5 mins to check the jobs that need to be run. For this, get the data from job model which need to be run in next 5 minutes.
  4. Update the status of job in the the job model.

For the model one can might use following:-

'use strict';
module.exports = (sequelize, DataTypes) => {
  const ScheduledEvent = sequelize.define('scheduled_events', {
    id: {
      type: DataTypes.UUID,
      defaultValue: DataTypes.UUIDV4,
      primaryKey: true,
      allowNull: false
    },
    event_id: {
      type: DataTypes.STRING,
      allowNull: false,
    },
    event: {
      type: DataTypes.JSONB,
      allowNull: false,
    },
    job_id: {
      type: DataTypes.STRING,
      allowNull: false,
    },
  }, {
    underscored: true,
  });
  ScheduledEvent.associate = function(models) {
    // associations can be defined here
    ScheduledEvent.belongsTo(models.User);
  };
  return ScheduledEvent;
};

There are other ways to go about this, but from scalability point of view this approach works good.

  • Related