Skip to content

Commit

Permalink
Merge branch 'hotfix/enable-entity' into 'master'
Browse files Browse the repository at this point in the history
Hotfix/enable entity

See merge request la-fabrique-numerique/biocarburants!243
  • Loading branch information
jfalxa committed Nov 14, 2024
2 parents bc5738c + ffa5565 commit 468a918
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 28 deletions.
2 changes: 1 addition & 1 deletion front/src/account/components/access-rights.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ export const EntityDialog = ({ onClose }: EntityDialogProps) => {
placeholder={t("Rechercher une société...")}
name="entity"
value={entity}
getOptions={common.findEntities}
getOptions={common.findEnabledEntities}
onChange={setEntity}
normalize={normalizeEntity}
/>
Expand Down
26 changes: 18 additions & 8 deletions front/src/carbure/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,31 @@ export function findCountries(query: string) {
.then(extract)
}

export function findEntities(query?: string, entity_type?: EntityType[]) {
export function findEntities(
query?: string,
filters?: { is_enabled?: boolean; entity_type?: EntityType[] }
) {
return api
.get<
Api<Entity[]>
>("/resources/entities", { params: { query, entity_type } })
>("/resources/entities", { params: { query, ...filters } })
.then(extract)
}

export function findEnabledEntities(query?: string) {
return findEntities(query, { is_enabled: true })
}

export function findBiofuelEntities(query?: string) {
return findEntities(query, [
EntityType.Producer,
EntityType.Trader,
EntityType.Operator,
EntityType.PowerOrHeatProducer,
])
return findEntities(query, {
is_enabled: true,
entity_type: [
EntityType.Producer,
EntityType.Trader,
EntityType.Operator,
EntityType.PowerOrHeatProducer,
],
})
}

export function findOperators(query?: string) {
Expand Down
42 changes: 23 additions & 19 deletions web/entity/services/enable_entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,42 @@

def enable_entity(entity, http_request):
# get entity admin
try:
right_request = UserRightsRequests.objects.get(entity=entity, role=UserRightsRequests.ADMIN, status="PENDING")
except Exception:
raise Exception("Cette société n'a pas de demande d'inscription en attente")

admin_user = right_request.user
# valid admin rights request
right_request.status = "ACCEPTED"
right_request.save()

# create user right
UserRights.objects.create(
entity=right_request.entity,
user=right_request.user,
role=right_request.role,
expiration_date=right_request.expiration_date,
right_requests = UserRightsRequests.objects.filter(
entity=entity,
role=UserRightsRequests.ADMIN,
status__in=["PENDING", "ACCEPTED"],
)

# validate admin rights request
right_requests.update(status="ACCEPTED")

# create user rights
UserRights.objects.bulk_create(
[
UserRights(
entity=req.entity,
user=req.user,
role=req.role,
expiration_date=req.expiration_date,
)
for req in right_requests
]
)

# enable entity
entity.is_enabled = True
entity.save()

# send email to user
subject = "Demande d'inscription de société validée"
subject = subject if CarbureEnv.is_prod else "TEST " + subject
recipient_list = [admin_user.email] if CarbureEnv.is_prod else ["[email protected]"]
recipient_list = [req.user.email for req in right_requests] or ["[email protected]"]
text_message = f"""
Bonjour,
Votre demande d'inscription pour la société {entity.name} a été validée par l'administration.
Vous pouvez désormais accéder à la société dans votre espace en tant qu'administrateur : {CarbureEnv.get_base_url()}/account
Pour plus d'information veuillez consulter notre guide d'utilisation : https://carbure-1.gitbook.io/faq/affichage/traduction
Pour plus d'information veuillez consulter notre guide d'utilisation : https://carbure-1.gitbook.io/faq/
Bien cordialement,
L'équipe CarbuRe
Expand Down
3 changes: 3 additions & 0 deletions web/resources/api/entities.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@

def get_entities(request):
q = request.GET.get("query", False)
is_enabled = request.GET.get("is_enabled") == "true"
entity_type = request.GET.getlist("entity_type")
entities = Entity.objects.all().order_by("name")
if q:
entities = entities.filter(name__icontains=q)
if entity_type:
entities = entities.filter(entity_type__in=entity_type)
if is_enabled:
entities = entities.filter(is_enabled=True)
sez = [{"entity_type": e.entity_type, "name": e.name, "id": e.id} for e in entities]
return JsonResponse({"status": "success", "data": sez})

0 comments on commit 468a918

Please sign in to comment.