feat: entities API

This commit is contained in:
Reto Aebersold 2023-11-15 09:14:24 +01:00 committed by Christian Cueni
parent 5916212857
commit 458d62daf3
8 changed files with 89 additions and 49 deletions

View File

@ -1,40 +1,20 @@
<script setup lang="ts">
import WizardPage from "@/components/onboarding/WizardPage.vue";
import ItDropdownSelect from "@/components/ui/ItDropdownSelect.vue";
import type { Ref } from "vue";
import { computed, ref, watch } from "vue";
import { useUserStore } from "@/stores/user";
import AvatarImage from "@/components/ui/AvatarImage.vue";
import { useFileUpload } from "@/composables";
import { useRoute } from "vue-router";
import { itPut, useFetch } from "@/fetchHelpers";
import { useTranslation } from "i18next-vue";
import { profileNextRoute } from "@/services/onboarding";
import { profileNextRoute, useEntities } from "@/services/onboarding";
const { t } = useTranslation();
type Organisation = {
organisation_id: number;
name_de: string;
name_fr: string;
name_it: string;
};
const user = useUserStore();
const route = useRoute();
const fetchResult = useFetch("/api/core/organisations/");
const organisationData: Ref<Organisation[] | null> = fetchResult.data;
const organisations = computed(() => {
if (organisationData.value) {
return organisationData.value.map((c) => ({
id: c.organisation_id,
name: c[`name_${user.language}`],
}));
}
return [];
});
const { organisations } = useEntities();
const selectedOrganisation = ref({
id: 0,
@ -69,10 +49,8 @@ watch(avatarFileInfo, (info) => {
console.log("fileInfo changed", info);
});
watch(selectedOrganisation, (organisation) => {
itPut("/api/core/me/", {
organisation: organisation.id,
});
watch(selectedOrganisation, async (organisation) => {
await user.setUserOrganisation(organisation.id);
});
const nextRoute = computed(() => {
@ -92,11 +70,7 @@ const nextRoute = computed(() => {
andere Personen einfacher finden.
</p>
<ItDropdownSelect
v-if="organisationData"
v-model="selectedOrganisation"
:items="organisations"
/>
<ItDropdownSelect v-model="selectedOrganisation" :items="organisations" />
<div class="mt-16 flex flex-col justify-between gap-12 lg:flex-row lg:gap-24">
<div>

View File

@ -1,3 +1,8 @@
import { useFetch } from "@/fetchHelpers";
import { useUserStore } from "@/stores/user";
import type { Ref } from "vue";
import { computed } from "vue";
export function profileNextRoute(courseType: string | string[]) {
if (courseType === "uk") {
return "setupComplete";
@ -7,3 +12,33 @@ export function profileNextRoute(courseType: string | string[]) {
}
return "";
}
export type Organisation = {
organisation_id: number;
name_de: string;
name_fr: string;
name_it: string;
};
export type Entities = {
organisations: Organisation[];
countries: string[];
};
export function useEntities() {
const user = useUserStore();
const fetchResult = useFetch("/api/core/entities/");
const entityData: Ref<Entities | null> = fetchResult.data;
const organisations = computed(() => {
if (entityData.value) {
return entityData.value.organisations.map((c) => ({
id: c.organisation_id,
name: c[`name_${user.language}`],
}));
}
return [];
});
return { organisations };
}

View File

@ -133,5 +133,9 @@ export const useUserStore = defineStore({
this.$state.language = language;
await itPost("/api/core/me/", { language }, { method: "PUT" });
},
async setUserOrganisation(organisation: number) {
this.$state.organisation = organisation;
await itPost("/api/core/me/", { organisation }, { method: "PUT" });
},
},
});

View File

@ -11,7 +11,8 @@ from django.views.decorators.csrf import csrf_exempt
from django_ratelimit.exceptions import Ratelimited
from graphene_django.views import GraphQLView
from vbv_lernwelt.api.user import list_organisations, me_user_view
from vbv_lernwelt.api.directory import list_entities
from vbv_lernwelt.api.user import me_user_view
from vbv_lernwelt.assignment.views import request_assignment_completion_status
from vbv_lernwelt.core.middleware.auth import django_view_authentication_exempt
from vbv_lernwelt.core.schema import schema
@ -99,7 +100,7 @@ urlpatterns = [
# user management
path("sso/", include("vbv_lernwelt.sso.urls")),
re_path(r'api/core/me/$', me_user_view, name='me_user_view'),
re_path(r'api/core/organisations/$', list_organisations, name='list_organisations'),
re_path(r'api/core/entities/$', list_entities, name='list_entities'),
re_path(r'api/core/login/$', django_view_authentication_exempt(vue_login),
name='vue_login'),

View File

@ -0,0 +1,16 @@
from rest_framework.decorators import api_view, permission_classes
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from vbv_lernwelt.core.models import Organisation
from vbv_lernwelt.core.serializers import OrganisationSerializer
from vbv_lernwelt.shop.models import Country
from vbv_lernwelt.shop.serializers import CountrySerializer
@api_view(["GET"])
@permission_classes([IsAuthenticated])
def list_entities(request):
organisations = OrganisationSerializer(Organisation.objects.all(), many=True).data
countries = CountrySerializer(Country.objects.all(), many=True).data
return Response({"organisations": organisations, "countries": countries})

View File

@ -5,16 +5,16 @@ from rest_framework.test import APITestCase
from vbv_lernwelt.core.models import User
class OrganisationViewTest(APITestCase):
class EntitiesViewTest(APITestCase):
def setUp(self) -> None:
self.user = User.objects.create_user(
"testuser", "test@example.com", "testpassword"
)
self.client.login(username="testuser", password="testpassword")
def test_list_organisations(self) -> None:
def test_list_entities(self) -> None:
# GIVEN
url = reverse("list_organisations")
url = reverse("list_entities")
# WHEN
response = self.client.get(url)
@ -22,8 +22,10 @@ class OrganisationViewTest(APITestCase):
# THEN
self.assertEqual(response.status_code, status.HTTP_200_OK)
organisations = response.data["organisations"]
self.assertEqual(
response.data[0],
organisations[0],
{
"organisation_id": 1,
"name_de": "andere Broker",
@ -31,3 +33,15 @@ class OrganisationViewTest(APITestCase):
"name_it": "altre Broker",
},
)
countries = response.data["countries"]
self.assertEqual(
countries[0],
{
"country_id": 1,
"name_de": "Afghanistan",
"name_fr": "Afghanistan",
"name_it": "Afghanistan",
},
)

View File

@ -1,17 +1,7 @@
from rest_framework.decorators import api_view
from rest_framework.response import Response
from vbv_lernwelt.core.models import Organisation
from vbv_lernwelt.core.serializers import OrganisationSerializer, UserSerializer
@api_view(["GET"])
def list_organisations(request):
if not request.user.is_authenticated:
return Response(status=403)
serializer = OrganisationSerializer(Organisation.objects.all(), many=True)
return Response(serializer.data)
from vbv_lernwelt.core.serializers import UserSerializer
@api_view(["GET", "PUT"])

View File

@ -1,6 +1,6 @@
from rest_framework import serializers
from .models import BillingAddress
from .models import BillingAddress, Country
class BillingAddressSerializer(serializers.ModelSerializer):
@ -21,3 +21,9 @@ class BillingAddressSerializer(serializers.ModelSerializer):
"company_city",
"company_country",
]
class CountrySerializer(serializers.ModelSerializer):
class Meta:
model = Country
fields = "__all__"