All files / ragchat-api/controllers User.js

88.88% Statements 48/54
54.54% Branches 6/11
100% Functions 2/2
88.88% Lines 48/54

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 551x 1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 3x     3x 3x 3x 3x 3x 1x 1x 1x 3x     1x 1x 1x 3x 2x 3x 3x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 2x     2x  
import User from "../models/User.js";
import { createRecord, findOneRecord } from "../utils/handleDB.js";
import { prefillDocumentObject, hideConfidentialFields } from '../utils/handleSchemes.js';
 
/** *******************************************************
 * CREATE ONE
 */
export const createUser = async (req, res, next) => {
  try {
    // autoverify if user-agent is Artillery
    const isArtilleryAgent = (req.get('user-agent') && req.get('user-agent').includes('Artillery') ? true : false);
    if (isArtilleryAgent) {
      req.body.verified = true;
    }
    // console.log("🚀 ~ createUser ~ isArtilleryAgent:", isArtilleryAgent);
    // return res.status(200).json({ message: 'tmp abort', isArtilleryAgent });
 
    // create user object
    const newRecord = await createRecord(User, prefillDocumentObject(User, req.body));
    // remember document but remove confidential info
    req.document = hideConfidentialFields(User, newRecord);
    // return if user-agent is Artillery
    if (isArtilleryAgent) {
      return res.status(201).json({ message: 'User created', document: req.document });
    }
    // continue to send verification
    next();
    // on error
  } catch (error) {
    next(error);
  };
};
 
 
 
/** *******************************************************
 * FIND USER BY MAIL
 */
export const prefetchUserByEmail = async (req, res, next) => {
  try {
    // search for matching document
    const foundUser = await findOneRecord(User, { email: req.body.email });
    // if user not found
    if (!foundUser) {
      // send message
      return res.status(404).json({ message: 'Unknown eMail address' });
    }
    // if user found, store in req for further computing
    req.document = foundUser;
    next();
  } catch (error) {
    next(error);
  }
};