diff --git a/apps/geo/mutations.py b/apps/geo/mutations.py index e43cf8090a..b896cee181 100644 --- a/apps/geo/mutations.py +++ b/apps/geo/mutations.py @@ -1,3 +1,4 @@ +from geo.tasks import load_geo_areas import graphene from geo.models import Region, AdminLevel @@ -134,6 +135,42 @@ def mutate(root, info, id): return PublishRegion(result=instance, errors=None, ok=True) +class ReTriggeredFailedRegion(graphene.Mutation): + class Arguments: + region_id = graphene.ID(required=True) + model = Region + errors = graphene.List(graphene.NonNull(CustomErrorType)) + ok = graphene.Boolean() + result = graphene.Field(RegionType) + + @staticmethod + def mutate(root, info, region_id): + instance = Region.objects.filter( + id=region_id + ).first() + error_data = [] + if instance is None: + error_data.append('Region does\'t exist') + elif instance.status == Region.Status.COMPLETED: + error_data.append('Geo Area already generated') + elif instance.created_by != info.context.user: + error_data.append('Authorized User only update region') + + if error_data: + return ReTriggeredFailedRegion(errors=[ + dict( + field='nonFieldErrors', + messages=error_data + ) + ], ok=False) + load_geo_areas(region_id) + return ReTriggeredFailedRegion( + result=instance, + error=None, + ok=True + ) + + class Mutation(): create_region = CreateRegion.Field() update_region = UpdateRegion.Field() @@ -141,3 +178,4 @@ class Mutation(): publish_region = PublishRegion.Field() update_admin_level = UpdateAdminLevel.Field() delete_admin_level = DeleteAdminLevel.Field() + retrigger_region = ReTriggeredFailedRegion.Field() diff --git a/apps/geo/tasks.py b/apps/geo/tasks.py index 8750e4f300..b895565499 100644 --- a/apps/geo/tasks.py +++ b/apps/geo/tasks.py @@ -220,6 +220,7 @@ def load_geo_areas(region_id): return False try: + region_id.status = Region.Status.INITIATED return_value = _load_geo_areas(region_id) except Exception: logger.error('Load Geo Areas', exc_info=True) diff --git a/schema.graphql b/schema.graphql index e6aebfb0ce..6a333bec40 100644 --- a/schema.graphql +++ b/schema.graphql @@ -5078,6 +5078,7 @@ type Mutation { publishRegion(id: ID!): PublishRegion updateAdminLevel(data: AdminLevelInputType!, id: ID!): UpdateAdminLevel deleteAdminLevel(adminLevelId: ID!): DeleteAdminLevel + retriggerRegion(regionId: ID!): ReTriggeredFailedRegion organizationCreate(data: OrganizationInputType!): OrganizationCreate createAssessmentRegSummaryIssue(data: AssessmentRegistrySummaryIssueCreateInputType!): AssessmentRegistryCreateIssue fileUpload(data: FileUploadInputType!): UploadFile @@ -5920,6 +5921,12 @@ type QuestionType { subSectorDisplay: EnumDescription } +type ReTriggeredFailedRegion { + errors: [GenericScalar!] + ok: Boolean + result: RegionType +} + type RecentActivityType { id: ID! createdAt: DateTime