avniproject / avni-server

Backend APIs for Avni
https://avniproject.org
GNU Affero General Public License v3.0
5 stars 25 forks source link

Fix duplicate privileges in webapp #721

Closed 1t5j0y closed 1 day ago

1t5j0y commented 2 months ago

Issue:

AC:

Old: Ignore

Context:

Via #683 and maybe other routes, it is possible for duplicate non-voided group_privilege records for the same entity to be created in the database. This leads to confusing behaviour.

In https://avni.freshdesk.com/a/tickets/3760 we saw that there are duplicate records for a few entities, some with allow as false and the other as true. On webapp user groups -> permissions screen, the true records were not shown. On client, since there is a record with allow as true, the privilege is allowed and sync tries to pull entities that the user does not have privileges to and is causing association errors during sync.

Further analysis might be required.

1t5j0y commented 2 months ago

This affects almost all orgs currently. Query to check impact:

with gp_counts as (select id, organisation_id, allow,
                          row_number()
                          over (partition by group_id, privilege_id, subject_type_id, program_id, program_encounter_type_id, encounter_type_id, checklist_detail_id order by id) as row_num
                   from group_privilege
                   where is_voided = false)
-- select * from gp_counts where row_num > 1;
select o.name, count(*) from gp_counts join  organisation o on o.id = organisation_id where row_num > 1 group by o.name order by o.name ;
1t5j0y commented 1 week ago

Queries in #746 to be executed (or alternate queries to remove dupes depending on environment) before deploying this.

petmongrels commented 1 week ago

Please add query to the release task if not added.

AchalaBelokar commented 2 days ago

errors-achalaissuebundle.csv

achalaissuebundle.zip