|
1 | 1 | import logging |
| 2 | +import typing |
2 | 3 |
|
3 | 4 | from celery import shared_task |
4 | 5 | from django.utils import timezone |
5 | 6 | from firebase_admin.db import Reference as FbReference |
6 | | - |
7 | 7 | from pyfirebase_mapswipe import models as firebase_models |
8 | 8 | from pyfirebase_mapswipe import utils as firebase_utils |
9 | | -from pyfirebase_mapswipe import models as firebase_models |
10 | 9 |
|
11 | | -from main.logging import log_extra |
| 10 | +from apps.project.models import FirebasePushStatusEnum, Organization |
12 | 11 | from main.config import Config |
13 | | -from apps.project.models import Organization, FirebasePushStatusEnum |
14 | | - |
| 12 | +from main.logging import log_extra |
15 | 13 |
|
16 | 14 | logger = logging.getLogger(__name__) |
17 | 15 |
|
18 | 16 |
|
19 | 17 | class InvalidOrganizationPushException(Exception): ... |
20 | 18 |
|
21 | 19 |
|
22 | | -def handle_new_organization_on_firebase(organization: Organization, orgnization_ref: FbReference): |
23 | | - |
| 20 | +def handle_new_organization_on_firebase(organization: Organization, organization_ref: FbReference): |
24 | 21 | organization_data = firebase_models.FbOrganisation( |
25 | 22 | name=organization.name, |
26 | 23 | nameKey=organization.unique_name, |
27 | | - description=organization.description, |
| 24 | + description=organization.description if organization.description else firebase_models.UNDEFINED, |
| 25 | + abbreviation=organization.abbreviation if organization.abbreviation else firebase_models.UNDEFINED, |
28 | 26 | isArchived=organization.is_archived, |
29 | | - archivedAt=organization.archived_at if organization.archived_at else firebase_models.UNDEFINED, |
30 | | - archivedBy=organization.archived_by_id if organization.archived_by else firebase_models.UNDEFINED, |
31 | 27 | ) |
32 | 28 |
|
33 | | - orgnization_ref.set( |
| 29 | + organization_ref.set( |
34 | 30 | value={ |
35 | 31 | **firebase_utils.serialize(organization_data), |
36 | 32 | }, |
37 | 33 | ) |
38 | 34 |
|
| 35 | + |
| 36 | +def handle_organization_update_on_firebase(organization: Organization, organization_ref: FbReference): |
| 37 | + organization_ref.update( |
| 38 | + value=firebase_utils.serialize( |
| 39 | + firebase_models.FbOrganisation( |
| 40 | + name=organization.name, |
| 41 | + nameKey=organization.unique_name, |
| 42 | + description=organization.description if organization.description else firebase_models.UNDEFINED, |
| 43 | + abbreviation=organization.abbreviation if organization.abbreviation else firebase_models.UNDEFINED, |
| 44 | + isArchived=organization.is_archived, |
| 45 | + ), |
| 46 | + ), |
| 47 | + ) |
| 48 | + |
| 49 | + |
39 | 50 | @shared_task |
40 | 51 | def push_organization_to_firebase(organization_id: int): |
41 | 52 | organization = Organization.objects.filter(id=organization_id).first() |
| 53 | + if not organization: |
| 54 | + return |
42 | 55 | organization.update_firebase_push_status(FirebasePushStatusEnum.PROCESSING) |
43 | 56 |
|
44 | 57 | try: |
|
0 commit comments