<commit_before>@Patients = new Meteor.Collection "patients"

Patients.allow
    update: (userId) -> authorizedUser userId
    remove: (userId) -> authorizedUser userId

Patients.helpers
    fullName: -> return "#{@firstName} #{@lastName}"

if Meteor.isServer

    mergeInstancesOfProcurement = (oldNumbers, newNumbers) ->
        instancesOfProcurement = oldNumbers.concat newNumbers
        # Based on http://coffeescriptcookbook.com/chapters/arrays/removing-duplicate-elements-from-arrays
        output = {}
        output[JSON.stringify(instancesOfProcurement[key])] = JSON.stringify(instancesOfProcurement[key]) for key in [0...instancesOfProcurement.length]
        return (JSON.parse(value) for key, value of output)

    generateUniqueShortId = (mrn) ->
        shortId = Random.id(6) # Generate a Short ID
        while Patients.findOne(shortId: shortId)
            shortId = Random.id(6) # Generate another Short ID
        shortId

    Meteor.methods
        patient: (patientAttributes) ->
            user = Meteor.user()
            patientWithSameMRN = Patients.findOne mrn: patientAttributes.mrn

            # Validation
            if not user then throw new Meteor.Error 401, "You need to log in to add patients"
            if not Roles.userIsInRole @userId, ["admin", "tech", "procurement-tech"] then throw new Meteor.Error 401, "You are not authorized to add patients"
            if not hasAttributes patientAttributes, ["firstName", "lastName", "mrn"]
                throw new Meteor.Error 422, "Patient first name, last name, and MRN are required"

            if patientWithSameMRN # Update Patient with any new Surgical Path #'s
                instancesOfProcurement = mergeInstancesOfProcurement patientWithSameMRN.instancesOfProcurement, patientAttributes.instancesOfProcurement
                Patients.update patientWithSameMRN._id, $set: instancesOfProcurement: instancesOfProcurement
                return patientWithSameMRN.shortId

            else # Create a new patient
                shortId = generateUniqueShortId patientAttributes.mrn
                patient = _.extend patientAttributes,
                    added: new Date().getTime()
                    addedBy: user._id
                    shortId: shortId
                patientId = Patients.insert(patient)
                return shortId

        lookupPatient: (mrn) ->
            if process.env.NODE_ENV is "development" and process.env.MOCK_EMMI is "true"
                return patient =
                    firstname: "Barbra"
                    lastname: "Hsistest"
                    middlename: "Qualif"
                    birthDate: "19830302"
            else
                if not Roles.userIsInRole Meteor.user(), ["admin", "tech", "procurement-tech"] then throw new Meteor.Error 401, "You are not authorized to lookup patients"
                client = new EMMI.Client EMMIClientSettings
                patient = (client.getPatient mrn).result
                _.each ["firstname", "lastname"], (property) ->
                    patient[property] = patient[property].toProperCase()
                patient
<commit_msg>only use /^[A-Z1-9]{6}$/ for kassebaum codes<commit_after>@Patients = new Meteor.Collection "patients"

Patients.allow
    update: (userId) -> authorizedUser userId
    remove: (userId) -> authorizedUser userId

Patients.helpers
    fullName: -> return "#{@firstName} #{@lastName}"

if Meteor.isServer

    mergeInstancesOfProcurement = (oldNumbers, newNumbers) ->
        instancesOfProcurement = oldNumbers.concat newNumbers
        # Based on http://coffeescriptcookbook.com/chapters/arrays/removing-duplicate-elements-from-arrays
        output = {}
        output[JSON.stringify(instancesOfProcurement[key])] = JSON.stringify(instancesOfProcurement[key]) for key in [0...instancesOfProcurement.length]
        return (JSON.parse(value) for key, value of output)

    generateUniqueShortId = (mrn) ->
        shortId = Random.id(6).toUpperCase() # Generate a Short ID
        while  shortId.match("0") or Patients.findOne(shortId: shortId)
            shortId = Random.id(6) # Generate another Short ID
        shortId

    Meteor.methods
        patient: (patientAttributes) ->
            user = Meteor.user()
            patientWithSameMRN = Patients.findOne mrn: patientAttributes.mrn

            # Validation
            if not user then throw new Meteor.Error 401, "You need to log in to add patients"
            if not Roles.userIsInRole @userId, ["admin", "tech", "procurement-tech"] then throw new Meteor.Error 401, "You are not authorized to add patients"
            if not hasAttributes patientAttributes, ["firstName", "lastName", "mrn"]
                throw new Meteor.Error 422, "Patient first name, last name, and MRN are required"

            if patientWithSameMRN # Update Patient with any new Surgical Path #'s
                instancesOfProcurement = mergeInstancesOfProcurement patientWithSameMRN.instancesOfProcurement, patientAttributes.instancesOfProcurement
                Patients.update patientWithSameMRN._id, $set: instancesOfProcurement: instancesOfProcurement
                return patientWithSameMRN.shortId

            else # Create a new patient
                shortId = generateUniqueShortId patientAttributes.mrn
                patient = _.extend patientAttributes,
                    added: new Date().getTime()
                    addedBy: user._id
                    shortId: shortId
                patientId = Patients.insert(patient)
                return shortId

        lookupPatient: (mrn) ->
            if process.env.NODE_ENV is "development" and process.env.MOCK_EMMI is "true"
                return patient =
                    firstname: "Barbra"
                    lastname: "Hsistest"
                    middlename: "Qualif"
                    birthDate: "19830302"
            else
                if not Roles.userIsInRole Meteor.user(), ["admin", "tech", "procurement-tech"] then throw new Meteor.Error 401, "You are not authorized to lookup patients"
                client = new EMMI.Client EMMIClientSettings
                patient = (client.getPatient mrn).result
                _.each ["firstname", "lastname"], (property) ->
                    patient[property] = patient[property].toProperCase()
                patient

{'fraction_non_alphanumeric': 0.08538122243226214, 'fraction_numerical': 0.007876496534341524, 'mean_word_length': 2.311945748565467, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, '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': '6785505', 'n_tokens_mistral': 1784, 'n_tokens_neox': 1641, 'n_words': 453}