All files / services/config-service/src/models User.js

55.31% Statements 78/141
100% Branches 1/1
0% Functions 0/3
55.31% Lines 78/141

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 1411x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x       1x 1x 1x                                           1x 1x 1x 1x 1x 1x                                                                               1x 1x 1x
const mongoose = require('mongoose');
 
const userSchema = new mongoose.Schema({
  googleId: {
    type: String,
    unique: true,
    sparse: true,
    index: true
  },
  googleEmail: {
    type: String,
    unique: true,
    sparse: true,
    lowercase: true,
    index: true
  },
  oidcId: {
    type: String,
    unique: true,
    sparse: true,
    index: true
  },
  oidcEmail: {
    type: String,
    unique: true,
    sparse: true,
    lowercase: true,
    index: true
  },
  email: {
    type: String,
    lowercase: true,
    trim: true
  },
  name: {
    type: String,
    required: true,
    trim: true
  },
  picture: {
    type: String,
    default: null
  },
  role: {
    type: String,
    enum: ['user', 'tester', 'admin'],
    default: 'user'
  },
  isActive: {
    type: Boolean,
    default: true
  },
  lastLogin: {
    type: Date,
    default: null
  },
  defaultOrganizationId: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'Organization',
    default: null
  }
}, {
  timestamps: true
});
 
userSchema.methods.toJSON = function() {
  const user = this.toObject();
  delete user.__v;
  return user;
};
 
userSchema.statics.findOrCreateFromGoogle = async function(profile) {
  let user = await this.findOne({ googleId: profile.googleId });
  
  if (user) {
    user.name = profile.name;
    user.picture = profile.picture;
    user.googleEmail = profile.email;
    user.lastLogin = new Date();
    await user.save();
    return user;
  }
  
  user = await this.create({
    googleId: profile.googleId,
    googleEmail: profile.email,
    email: profile.email,
    name: profile.name,
    picture: profile.picture,
    lastLogin: new Date()
  });
  
  return user;
};
 
// Liaison par email : un login SSO (OIDC) avec un email déjà connu (Google ou
// OIDC) est rattaché au même compte. Sinon, le compte est créé (le premier
// utilisateur de la plateforme devient admin).
userSchema.statics.findOrCreateFromOidc = async function(profile) {
  const email = String(profile.email || '').toLowerCase();

  let user = await this.findOne({ oidcId: profile.oidcId });
  if (user) {
    user.name = profile.name || user.name;
    user.picture = profile.picture || user.picture;
    if (email) user.oidcEmail = email;
    user.lastLogin = new Date();
    await user.save();
    return user;
  }

  if (email) {
    user = await this.findOne({
      $or: [{ email }, { googleEmail: email }, { oidcEmail: email }]
    });
  }
  if (user) {
    user.oidcId = profile.oidcId;
    user.oidcEmail = email;
    user.name = profile.name || user.name;
    user.picture = profile.picture || user.picture;
    user.lastLogin = new Date();
    await user.save();
    return user;
  }

  const isFirstUser = (await this.countDocuments()) === 0;
  user = await this.create({
    oidcId: profile.oidcId,
    oidcEmail: email,
    email,
    name: profile.name,
    picture: profile.picture,
    lastLogin: new Date(),
    role: isFirstUser ? 'admin' : 'user'
  });

  return user;
};
 
module.exports = mongoose.model('User', userSchema);