from django.db import migrations def merge_legacy_accounting_unit(apps, schema_editor): Unit = apps.get_model("staff", "OrganizationalUnit") Membership = apps.get_model("staff", "UnitMembership") StaffProfile = apps.get_model("staff", "StaffProfile") TicketCategory = apps.get_model("tickets", "TicketCategory") Ticket = apps.get_model("tickets", "Ticket") GroupDispatch = apps.get_model("tickets", "GroupDispatch") ContactGroup = apps.get_model("customers", "ContactGroup") CustomerProfile = apps.get_model("customers", "CustomerProfile") SMSTemplate = apps.get_model("notifications", "SMSTemplate") TicketMagicLink = apps.get_model("accounts", "TicketMagicLink") legacy = Unit.objects.filter(slug="accounting").first() canonical = Unit.objects.filter(slug="finance").first() if legacy is None: return if canonical is None: legacy.slug = "finance" legacy.name = "مالی / حسابداری" legacy.description = "واحد مالی و حسابداری (یک مفهوم سازمانی)" legacy.is_active = True legacy.accepts_external_tickets = True legacy.show_in_customer_form = True legacy.save() return canonical.permissions.add(*legacy.permissions.all()) TicketCategory.objects.filter(unit=legacy).update(unit=canonical) Ticket.objects.filter(owning_unit=legacy).update(owning_unit=canonical) Ticket.objects.filter(assigned_unit=legacy).update(assigned_unit=canonical) GroupDispatch.objects.filter(unit=legacy).update(unit=canonical) TicketMagicLink.objects.filter(created_unit=legacy).update(created_unit=canonical) StaffProfile.objects.filter(primary_unit=legacy).update(primary_unit=canonical) for membership in Membership.objects.filter(unit=legacy): existing = Membership.objects.filter(user_id=membership.user_id, unit=canonical).first() if existing is None: membership.unit = canonical membership.save(update_fields=["unit", "updated_at"]) continue existing.permissions.add(*membership.permissions.all()) existing.is_active = existing.is_active or membership.is_active existing.is_primary = existing.is_primary or membership.is_primary if membership.ticket_scope == "UNIT": existing.ticket_scope = "UNIT" existing.save(update_fields=["is_active", "is_primary", "ticket_scope", "updated_at"]) membership.delete() for group in ContactGroup.objects.filter(unit=legacy): existing = ContactGroup.objects.filter(unit=canonical, name=group.name).first() if existing is None: group.unit = canonical group.save(update_fields=["unit", "updated_at"]) continue for profile in CustomerProfile.objects.filter(groups=group): profile.groups.add(existing) GroupDispatch.objects.filter(contact_group=group).update(contact_group=existing) group.delete() for template in SMSTemplate.objects.filter(unit=legacy): if SMSTemplate.objects.filter(unit=canonical, event_type=template.event_type).exists(): template.delete() else: template.unit = canonical template.save(update_fields=["unit", "updated_at"]) legacy.delete() class Migration(migrations.Migration): dependencies = [("tickets", "0012_groupdispatch_operation_permissions_and_seed_org")] operations = [migrations.RunPython(merge_legacy_accounting_unit, migrations.RunPython.noop)]