{"size": 1125, "ext": "js", "max_stars_repo_path": "lib/services/LogService.js", "max_stars_repo_name": "Alchemy-Traction/traction", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "lib/services/LogService.js", "max_issues_repo_name": "Alchemy-Traction/traction", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "lib/services/LogService.js", "max_forks_repo_name": "Alchemy-Traction/traction", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 40.1785714286, "max_line_length": 114, "alphanum_fraction": 0.7146666667}
const Log = require('../models/Log');
const User = require('../models/User');
const Habit = require('../models/Habit');
const compareFaces = require('../utils/aws-utils/compareFaces');
const detectLabels = require('../utils/aws-utils/detectLabels');
const mungeLabels = require('../utils/aws-utils/mungeLabels');
const sendSms = require('../utils/sendSms');


module.exports = class LogService {
  static async create(phoneNumber, habitImageUrl) {
    const user = await User
      .findByPhoneNumber(phoneNumber);

    const labels = await detectLabels(habitImageUrl);
    const mungedLabels = await mungeLabels(labels);
    const matchedHabit = await Habit.confirmLabelsbyUserId(user.id, mungedLabels);
    const facesMatch = await compareFaces(user.userPhotoUrl, habitImageUrl);

    if(facesMatch && matchedHabit) {
      await Log.insert(user.id, matchedHabit.id, habitImageUrl); 
      sendSms(phoneNumber, 'Great job! You completed your habit for the day. See you tomorrow!');
    } else {
      sendSms(phoneNumber, 'That photo didn\'t match the labels or the face that we expected. Please try again.');
    }
  }
};
