Skip to content
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

feat: update organization sales/supplied average to display sales if less than 3 years of supplied #2451

Merged
merged 6 commits into from
Feb 11, 2025

Conversation

emi-hi
Copy link
Collaborator

@emi-hi emi-hi commented Feb 10, 2025

-if less than 3 years of records exist for supplied, passes sales instead (dont just show 0)
-new @Property to display whether its a sales or supplied average
-updates frontend to display whether its sales or supplied

…s instead

-new @Property to display whether its a sales or supplied average
-updates frontend to display whether its sales or supplied
@emi-hi emi-hi requested a review from tim738745 February 10, 2025 21:41
Copy link
Collaborator

@tim738745 tim738745 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@emi-hi, I think there are some references to get_avg_ldv_sales() in the backend that need to be addressed as well (line 173 in models/model_year_report.py, as well as line 181 in viewsets/model_year_report.py).

Also, I was thinking we don't need to update the frontend avgLdvSales references; maybe, in the OrganizationSerializer (line 23 of serializers/organization.py), we could do:

class OrganizationSerializer(serializers.ModelSerializer):
  ...
  avg_ldv_sales = serializers.SerializerMethodField()
  supplied_or_sales = serializers.SerializerMethodField()
  ...
  def get_avg_ldv_sales(self, obj):
        avg_sales, _ =  obj.get_avg_ldv_sales()
        return avg_sales

  def get_supplied_or_sales(self, obj):
        _ , supplied_or_sales = obj.get_avg_ldv_sales()
        return supplied_or_sales
  ...
  class Meta:
        model = Organization
        fields = (... "avg_ldv_sales", "supplied_or_sales",...)

And on the frontend, in the VehicleSupplierDetailsPage, just have what you had before:
<h4 className="d-inline">3 Year Average Vehicles {details.suppliedOrSales}: </h4>

Edit: Better yet, in order to avoid unnecessarily calling get_avg_ldv_sales() a second time for each use of the serializer, we can override the to_representation method of the serializer (https://www.django-rest-framework.org/api-guide/serializers/#overriding-serialization-and-deserialization-behavior).

So we can do:

class OrganizationSerializer(serializers.ModelSerializer):
    """
    Serializer for the Supplier
    Loads most of the fields and the balance for the Supplier
    """
    organization_address = serializers.SerializerMethodField()
    ldv_sales = OrganizationLDVSalesSerializer(many=True)
    first_model_year = serializers.SerializerMethodField()

    def get_organization_address(self, obj):
        """
        Loads the latest valid address for the organization
        """
        if obj.organization_address is None:
            return None

        serializer = OrganizationAddressSerializer(
            obj.organization_address, read_only=True, many=True
        )

        return serializer.data
    
    def get_first_model_year(self, obj):
        if obj.first_model_year is not None:
            return obj.first_model_year.name
        return None
    
    def to_representation(self, instance):
        ret = super().to_representation(instance)
        x, y = instance.get_avg_ldv_sales()
        ret["avg_ldv_sales"] = x
        ret["supplied_or_sales"] = y
        return ret

    class Meta:
        model = Organization
        fields = (
            'id', 'name', 'create_timestamp', 'organization_address',
            'balance', 'is_active', 'short_name', 'is_government',
            'supplier_class', 'ldv_sales',
            'has_submitted_report', 'first_model_year', 'has_report',
        )

@tim738745 tim738745 self-requested a review February 11, 2025 20:47
@tim738745 tim738745 merged commit 01bcd9f into bcgov:release-1.66.0 Feb 11, 2025
7 checks passed
kuanfandevops added a commit that referenced this pull request Feb 11, 2025
* initial updates for 1.66.0

* add oc-client image

* feat: update organization sales/supplied average to display sales if less than 3 years of supplied (#2451)

* feat: if less than 3 years of records exist for supplied, passes sales instead
-new @Property to display whether its a sales or supplied average
-updates frontend to display whether its sales or supplied

* chore: removes duplicate code

* chore: updates code as per Tim's request!

* chore: removes unnecessary None, None

* fix: fixes woopsie

* fix typo

---------

Co-authored-by: tim738745 <[email protected]>

* 2439, 2440 Bug-fix: "year" at backend may not be int (#2452)

* Bug-fix: "year" at backend may not be int

* Ensure "year" is int in get_avg_ldv_sales

---------

Co-authored-by: Emily <[email protected]>
Co-authored-by: tim738745 <[email protected]>
Co-authored-by: Roger Leung <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants