<commit_before>import Parse from 'parse/react-native';
import realm from '../data/Realm';
import {currentUser} from '../api/Account';
import { adminRole } from '../api/Roles';
import crypto from 'crypto-js';
import async from 'async';
import _ from 'lodash';

const Submission = Parse.Object.extend('Submission');
/**
 * create a submission Object with acl assigned
 *
 * @param {string} id, the unique id for the realm record
 * @param {string} formId, the unique id for the parse form record
 * @param {object} answers, the answers to the current form
 * @param {object} user, the current parse user saved to AsyncStorage
 * @param {object} role, the adminRole to be assigned to the object
 */
Submission.create = function create(uniqueId, formId, answers, user, role) {
  const submission = new Submission();
  submission.set('uniqueId', uniqueId);
  submission.set('formId', formId);
  submission.set('answers', answers);
  submission.set('userId', user.id);
  const acl = new Parse.ACL();
  acl.setReadAccess(user, true);
  acl.setWriteAccess(user, true);
  acl.setRoleReadAccess(role, true);
  acl.setRoleWriteAccess(role, true);
  submission.setACL(acl);
  return submission;
};

// Fetches the cached submissions related to a specific form
export function loadCachedSubmissions(formId) {
  return realm.objects('Submission').filtered(`formId = "${formId}"`).sorted('created');
}

/**
 * user may only have one submission for each formId
 *
 * @param {string} formId, the unique id for the parse form record
 * @param {object} user, the current parse user saved to AsyncStorage
 */
function genSubmissionId(formId, user, done) {
  const str = `${user.id}${formId}`;
  const id = crypto.MD5(str).toString();
  done(null, id);
}

/**
 * wrapper around Submission.create what will also save asychronously
 *
 * @param {string} id, the unique id for the realm record
 * @param {string} formId, the unique id for the parse form record<commit_msg>store ref to user and not the string<commit_after>import Parse from 'parse/react-native';
import realm from '../data/Realm';
import {currentUser} from '../api/Account';
import { adminRole } from '../api/Roles';
import crypto from 'crypto-js';
import async from 'async';
import _ from 'lodash';

const Submission = Parse.Object.extend('Submission');
/**
 * create a submission Object with acl assigned
 *
 * @param {string} id, the unique id for the realm record
 * @param {string} formId, the unique id for the parse form record
 * @param {object} answers, the answers to the current form
 * @param {object} user, the current parse user saved to AsyncStorage
 * @param {object} role, the adminRole to be assigned to the object
 */
Submission.create = function create(uniqueId, formId, answers, user, role) {
  const submission = new Submission();
  submission.set('uniqueId', uniqueId);
  submission.set('formId', formId);
  submission.set('answers', answers);
  submission.set('userId', user);
  const acl = new Parse.ACL();
  acl.setReadAccess(user, true);
  acl.setWriteAccess(user, true);
  acl.setRoleReadAccess(role, true);
  acl.setRoleWriteAccess(role, true);
  submission.setACL(acl);
  return submission;
};

// Fetches the cached submissions related to a specific form
export function loadCachedSubmissions(formId) {
  return realm.objects('Submission').filtered(`formId = "${formId}"`).sorted('created');
}

/**
 * user may only have one submission for each formId
 *
 * @param {string} formId, the unique id for the parse form record
 * @param {object} user, the current parse user saved to AsyncStorage
 */
function genSubmissionId(formId, user, done) {
  const str = `${user.id}${formId}`;
  const id = crypto.MD5(str).toString();
  done(null, id);
}

/**
 * wrapper around Submission.create what will also save asychronously
 *
 * @param {string} id, the unique id for the realm record
 * @param {string} formId, the unique id for the parse form record
{'fraction_non_alphanumeric': 0.1258937691521961, 'fraction_numerical': 0.0005107252298263534, 'mean_word_length': 4.701601164483261, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6573404', 'n_tokens_mistral': 1158, 'n_tokens_neox': 1155, 'n_words': 422}