def test_create(session, participant_role):
    from dispatch.participant.service import create
    from dispatch.participant.models import ParticipantCreate

    participant_in = ParticipantCreate(participant_role=[participant_role])
    participant = create(db_session=session, participant_in=participant_in)
    assert participant
def test_get_or_create(session, incident, individual_contact,
                       participant_role):
    from dispatch.participant.service import create
    from dispatch.participant.models import Participant, ParticipantCreate

    participant = (session.query(Participant).filter(
        Participant.incident_id == incident.id).filter(
            Participant.individual_contact_id ==
            individual_contact.id).one_or_none())

    if not participant:
        participant_in = ParticipantCreate(participant_role=[participant_role])
        participant = create(db_session=session, participant_in=participant_in)

    assert participant
예제 #3
0
def create(
    *,
    db_session,
    incident_priority: str,
    incident_type: str,
    reporter_email: str,
    title: str,
    status: str,
    description: str,
) -> Incident:
    participants = []

    # TODO should some of this logic be in the incident_create_flow_instead? (kglisson)
    incident_priority = incident_priority_service.get_by_name(
        db_session=db_session, name=incident_priority["name"])

    incident_type = incident_type_service.get_by_name(
        db_session=db_session, name=incident_type["name"])

    commander_email = resolve_incident_commander_email(
        db_session,
        reporter_email,
        incident_type.name,
        incident_priority.name,
        "",
        title,
        description,
    )

    commander_info = individual_service.resolve_user_by_email(commander_email)

    incident_commander_role = participant_role_service.create(
        db_session=db_session, role=ParticipantRoleType.incident_commander)

    commander_participant = participant_service.create(
        db_session=db_session, participant_role=[incident_commander_role])

    commander = individual_service.get_or_create(
        db_session=db_session,
        email=commander_info["email"],
        name=commander_info["fullname"],
        weblink=commander_info["weblink"],
    )

    incident_reporter_role = participant_role_service.create(
        db_session=db_session, role=ParticipantRoleType.reporter)

    if reporter_email == commander_email:
        commander_participant.participant_role.append(incident_reporter_role)
    else:
        reporter_participant = participant_service.create(
            db_session=db_session, participant_role=[incident_reporter_role])
        reporter_info = individual_service.resolve_user_by_email(
            reporter_email)
        reporter = individual_service.get_or_create(
            db_session=db_session,
            email=reporter_info["email"],
            name=reporter_info["fullname"],
            weblink=commander_info["weblink"],
        )
        reporter.participant.append(reporter_participant)
        db_session.add(reporter)
        participants.append(reporter_participant)

    participants.append(commander_participant)
    incident = Incident(
        title=title,
        description=description,
        status=status,
        incident_priority=incident_priority,
        incident_type=incident_type,
        participants=participants,
    )

    commander.participant.append(commander_participant)
    db_session.add(commander)
    db_session.add(incident)
    db_session.commit()
    return incident