-
Notifications
You must be signed in to change notification settings - Fork 336
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(asset_location): added duty_staff endpoint #1689
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5fd0e78
fix(asset_location): added duty_staff endpoint
aeswibon 825d68b
fix(asset): replaced bulk operation with individual operation
aeswibon 38081e9
fix(asset): fixed migrations
aeswibon 8e03aee
fix(asset): fixed linting errors
aeswibon 7e690cf
fix(facility): rebased with master branch
aeswibon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,12 +42,14 @@ | |
Asset, | ||
AssetAvailabilityRecord, | ||
AssetLocation, | ||
AssetLocationDutyStaff, | ||
AssetService, | ||
AssetTransaction, | ||
ConsultationBedAsset, | ||
UserDefaultAssetLocation, | ||
) | ||
from care.facility.models.asset import AssetTypeChoices, StatusChoices | ||
from care.users.api.serializers.user import UserBaseMinimumSerializer | ||
from care.users.models import User | ||
from care.utils.assetintegration.asset_classes import AssetClasses | ||
from care.utils.assetintegration.base import BaseAssetIntegration | ||
|
@@ -118,6 +120,82 @@ | |
def perform_create(self, serializer): | ||
serializer.save(facility=self.get_facility()) | ||
|
||
@extend_schema(tags=["asset_location"]) | ||
@action(methods=["POST"], detail=True) | ||
def duty_staff(self, request, facility_external_id, external_id): | ||
""" | ||
Endpoint for assigning staffs to asset location | ||
""" | ||
|
||
asset: AssetLocation = self.get_object() | ||
duty_staff = request.data.get("duty_staff") | ||
|
||
if not duty_staff: | ||
return Response(status=status.HTTP_204_NO_CONTENT) | ||
|
||
query = AssetLocationDutyStaff.objects.filter( | ||
asset_location=asset, user__id=duty_staff, deleted=False | ||
) | ||
|
||
if query.exists(): | ||
raise ValidationError( | ||
{"duty_staff": "Staff already assigned to the location"} | ||
) | ||
|
||
user = User.objects.filter(id=duty_staff, home_facility=asset.facility) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use first() here itself. no need for 2 queries. |
||
if not user.exists(): | ||
raise ValidationError( | ||
{"duty_staff": "Staff does not belong to the facility"} | ||
) | ||
|
||
AssetLocationDutyStaff.objects.create( | ||
asset_location=asset, user=user.first(), created_by=request.user | ||
) | ||
|
||
return Response(status=status.HTTP_201_CREATED) | ||
|
||
@extend_schema(tags=["asset_location"]) | ||
@duty_staff.mapping.get | ||
def duty_staff_get(self, request, facility_external_id, external_id): | ||
""" | ||
Endpoint for getting staffs from asset location | ||
""" | ||
|
||
asset: AssetLocation = self.get_object() | ||
|
||
duty_staff = User.objects.filter( | ||
id__in=AssetLocationDutyStaff.objects.filter( | ||
asset_location=asset, deleted=False | ||
).values_list("user__id", flat=True) | ||
) | ||
|
||
return Response( | ||
UserBaseMinimumSerializer(duty_staff, many=True).data, | ||
status=status.HTTP_200_OK, | ||
) | ||
|
||
@extend_schema(tags=["asset_location"]) | ||
@duty_staff.mapping.delete | ||
def duty_staff_delete(self, request, facility_external_id, external_id): | ||
""" | ||
Endpoint for removing staffs from asset location | ||
""" | ||
|
||
asset: AssetLocation = self.get_object() | ||
|
||
if "duty_staff" not in request.data: | ||
raise ValidationError({"duty_staff": "Staff is required"}) | ||
|
||
duty_staff = request.data.get("duty_staff") | ||
if not duty_staff: | ||
raise ValidationError({"duty_staff": "Staff is required"}) | ||
|
||
AssetLocationDutyStaff.objects.filter( | ||
asset_location=asset, user__id=duty_staff | ||
).update(deleted=True) | ||
|
||
return Response(status=status.HTTP_204_NO_CONTENT) | ||
|
||
|
||
class AssetFilter(filters.FilterSet): | ||
facility = filters.UUIDFilter(field_name="current_location__facility__external_id") | ||
|
@@ -260,7 +338,9 @@ | |
queryset = self.filter_queryset(self.get_queryset()).values(*mapping.keys()) | ||
pretty_mapping = Asset.CSV_MAKE_PRETTY.copy() | ||
return render_to_csv_response( | ||
queryset, field_header_map=mapping, field_serializer_map=pretty_mapping | ||
queryset, | ||
field_header_map=mapping, | ||
field_serializer_map=pretty_mapping, | ||
) | ||
|
||
return super(AssetViewSet, self).list(request, *args, **kwargs) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
care/facility/migrations/0398_assetlocationdutystaff_assetlocation_users.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
# Generated by Django 4.2.5 on 2023-11-17 13:25 | ||
|
||
import uuid | ||
|
||
import django.db.models.deletion | ||
from django.conf import settings | ||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
migrations.swappable_dependency(settings.AUTH_USER_MODEL), | ||
("facility", "0397_truncate_discharge_time"), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name="AssetLocationDutyStaff", | ||
fields=[ | ||
( | ||
"id", | ||
models.BigAutoField( | ||
auto_created=True, | ||
primary_key=True, | ||
serialize=False, | ||
verbose_name="ID", | ||
), | ||
), | ||
( | ||
"external_id", | ||
models.UUIDField(db_index=True, default=uuid.uuid4, unique=True), | ||
), | ||
( | ||
"created_date", | ||
models.DateTimeField(auto_now_add=True, db_index=True, null=True), | ||
), | ||
( | ||
"modified_date", | ||
models.DateTimeField(auto_now=True, db_index=True, null=True), | ||
), | ||
("deleted", models.BooleanField(db_index=True, default=False)), | ||
( | ||
"asset_location", | ||
models.ForeignKey( | ||
on_delete=django.db.models.deletion.CASCADE, | ||
to="facility.assetlocation", | ||
), | ||
), | ||
( | ||
"created_by", | ||
models.ForeignKey( | ||
on_delete=django.db.models.deletion.PROTECT, | ||
related_name="+", | ||
to=settings.AUTH_USER_MODEL, | ||
), | ||
), | ||
( | ||
"user", | ||
models.ForeignKey( | ||
on_delete=django.db.models.deletion.CASCADE, | ||
to=settings.AUTH_USER_MODEL, | ||
), | ||
), | ||
], | ||
options={ | ||
"abstract": False, | ||
}, | ||
), | ||
migrations.AddField( | ||
model_name="assetlocation", | ||
name="users", | ||
field=models.ManyToManyField( | ||
blank=True, | ||
related_name="duty_staff", | ||
through="facility.AssetLocationDutyStaff", | ||
to=settings.AUTH_USER_MODEL, | ||
), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# Generated by Django 4.2.5 on 2023-12-15 03:15 | ||
|
||
from django.db import migrations | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("facility", "0398_assetlocationdutystaff_assetlocation_users"), | ||
("facility", "0402_patientconsultation_new_discharge_reason"), | ||
] | ||
|
||
operations = [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This needs to be performed first, we need to check if a use exists before doing anything else, we also need to ensure that the id is an integer or perform some validation on it.