def associate_barcode_to_survey_id(self, ag_login_id, participant_name,
                                       barcode, survey_id):
        """Associate a barcode to a survey ID

        Parameters
        ----------
        ag_login_id : str
            A valid AG login ID
        participant_name : str
            The name of a participant associated with the login
        barcode : str
            A valid barcode associated with the login
        survey_id : str
            A valid survey ID
        """
        with TRN:
            # test first if the barcode is already associated to a participant
            sql = """SELECT ag_login_id, participant_name, barcode
                     FROM ag.ag_login_surveys
                     JOIN ag.source_barcodes_surveys USING(survey_id)
                     WHERE ag_login_id=%s
                        AND participant_name=%s
                        AND barcode=%s"""
            TRN.add(sql, [ag_login_id, participant_name, barcode])
            results = TRN.execute_fetchflatten()

            if len(results) == 0:
                # this implies the barcode was unassigned, and this is a new
                # assignment.

                # Let's verify the barcode is associated to the kit and login
                sql = """SELECT 1
                         FROM ag.ag_login
                         JOIN ag.ag_kit USING (ag_login_id)
                         JOIN ag.ag_kit_barcodes USING (ag_kit_id)
                         WHERE ag_login_id=%s
                            AND barcode=%s"""

                TRN.add(sql, [ag_login_id, barcode])
                results = TRN.execute_fetchflatten()

                if len(results) == 0:
                    # the barcode is not part of a kit with the login ID
                    raise ValueError("Unexpected barcode / kit relationship")

                # the barcode should also not already be linked to a
                # participant within the kit
                sql = """SELECT 1
                         FROM ag.ag_login_surveys
                         JOIN ag.source_barcodes_surveys USING(survey_id)
                         WHERE ag_login_id=%s
                            AND barcode=%s"""
                TRN.add(sql, [ag_login_id, barcode])
                results = TRN.execute_fetchflatten()

                if len(results) > 0:
                    # the barcode is already assigned to someone on the kit
                    raise ValueError("Barcode already assigned")

            sql = """INSERT INTO ag_login_surveys
                     (ag_login_id, survey_id, participant_name)
                     VALUES (%s, %s, %s)"""
            TRN.add(sql, [ag_login_id, survey_id, participant_name])

            sql = """INSERT INTO ag.source_barcodes_surveys
                     (survey_id, barcode)
                     VALUES (%s, %s)"""
            TRN.add(sql, [survey_id, barcode])