def lever_rapport_action_strategy(soknad, action, data):
    rapport = RapportRepo.find_by_where("soknad_id", soknad.id)
    if len(rapport) != 1:
        abort(400, __error__=['Kan ikke finne rapport'])

    errors = rapport[0].validate_rapport_for_levering()
    if errors:
        abort(400, __error__=errors)

    soknad.status = action.get_end_state().id
    SoknadRepo.save(soknad, autocommit=False)

    # Arkivering
    # ###########
    organisation = get_organisation(soknad.organisation_id, request.cookies)
    person = get_person(soknad.person_id, request.cookies)

    csv_file_content = ExportRapporterResource().get(soknad_id=soknad.id, rapport_id=rapport[0].id).data

    save_journalpost_for_rapport(soknad, rapport[0], organisation, person, csv_file_content,
                                 u"soknad-%s-rapport-%s-%s.pdf" % (soknad.id, rapport[0].id, datetime.now().isoformat()))

    current_app.db_session.commit()

    # Sending mail after save to avoid that mail integration errors make it impossible to perform action.
    # It also makes sense to send mail only when we know that the action is successful
    send_email_to_soker_on_action_lever_rapport(soknad)
    send_email_to_saksbehandler_on_action_lever_rapport(soknad)
def underkjenn_rapport_action_strategy(soknad, action, data):
    rapport = RapportRepo.find_by_where("soknad_id", soknad.id)
    if len(rapport) != 1:
        abort(400, __error__=['Kan ikke finne rapport'])

    if data:
        validator = BaseValidator(data)
        validator.validate_le_max_length("saksbehandler_kommentar", 300)
        if validator.has_errors():
            abort(400, __error__=validator.errors)

        rapport[0].saksbehandler_kommentar = data.get("saksbehandler_kommentar")

        RapportRepo.save(rapport[0])
        # Sending mail after save to avoid that mail integration errors make it impossibv to perform action.
        # It also makes sense to send mail only when we know that the action is successful
        send_email_to_soker_on_action_underkjenn(soknad, data.get("saksbehandler_kommentar"))

    vedtak = soknad.nyeste_fattet_vedtak()
    if vedtak.tilskuddsordning_type == FORSKUDDSUTBETALING:
        soknad.status = SoknadStateMachine.s_tilbakebetaling_kreves.id
    elif vedtak.tilskuddsordning_type == ETTERSKUDDSUTBETALING:
        soknad.status = SoknadStateMachine.s_trukket.id
    SoknadRepo.save(soknad)