From 82936633f177e3f66a104e5446e21537f33f444e Mon Sep 17 00:00:00 2001 From: eee4 <41441600+eee4@users.noreply.github.com> Date: Mon, 19 May 2025 01:49:33 +0200 Subject: [PATCH] feat: return organisationId if user is an organization this will help to determine if an event is created by the user or not --- WebApp/DTOs/UserSummaryDto.cs | 11 +++++++++++ WebApp/Endpoints/AuthEndpoints.cs | 3 +++ WebApp/Endpoints/GeneralUseHelperFunctions.cs | 6 ++++++ WebApp/Mapping/UserMapping.cs | 13 +++++++++++++ 4 files changed, 33 insertions(+) diff --git a/WebApp/DTOs/UserSummaryDto.cs b/WebApp/DTOs/UserSummaryDto.cs index 6688eab..63514d4 100644 --- a/WebApp/DTOs/UserSummaryDto.cs +++ b/WebApp/DTOs/UserSummaryDto.cs @@ -11,4 +11,15 @@ namespace WebApp.DTOs [Required] DateTime CreatedAt, [Required] bool isOrganisation ); + + public record class UserSummaryWithOrgIdDto + ( + [Required] int UserId, + [Required] string Email, + [Required] string FirstName, + [Required] string LastName, + [Required] DateTime CreatedAt, + [Required] bool isOrganisation, + int? OrganisationId + ); } diff --git a/WebApp/Endpoints/AuthEndpoints.cs b/WebApp/Endpoints/AuthEndpoints.cs index 6344dbb..3668d6c 100644 --- a/WebApp/Endpoints/AuthEndpoints.cs +++ b/WebApp/Endpoints/AuthEndpoints.cs @@ -71,7 +71,10 @@ namespace WebApp.Endpoints return Results.Json(new {message = "No user found."}, statusCode: 404); } + Organisation? org = await guh.GetOrganisationFromUserId(user.UserId); + if (org is not null) return Results.Ok(user.ToUserSummaryWithOrgIdDto(org.OrganisationId)); return Results.Ok(user.ToUserSummaryDto()); + }) .WithName(GetUserEndpointName); diff --git a/WebApp/Endpoints/GeneralUseHelperFunctions.cs b/WebApp/Endpoints/GeneralUseHelperFunctions.cs index 96af125..6a12bfb 100644 --- a/WebApp/Endpoints/GeneralUseHelperFunctions.cs +++ b/WebApp/Endpoints/GeneralUseHelperFunctions.cs @@ -54,6 +54,12 @@ public class GeneralUseHelpers return org; } + async public Task GetOrganisationFromUserId(int userId) + { + Organisation? org = await _context.Organisations.FirstOrDefaultAsync(o => o.UserId == userId); + return org; + } + public string? GetTokenStrFromHTTPContext(HttpContext httpContext) { var cookies = httpContext.Request.Cookies; diff --git a/WebApp/Mapping/UserMapping.cs b/WebApp/Mapping/UserMapping.cs index d92bb84..3cf057c 100644 --- a/WebApp/Mapping/UserMapping.cs +++ b/WebApp/Mapping/UserMapping.cs @@ -16,5 +16,18 @@ namespace WebApp.Mapping user.IsOrganisation ); } + + public static UserSummaryWithOrgIdDto ToUserSummaryWithOrgIdDto(this User user, int OrganisationId) + { + return new UserSummaryWithOrgIdDto( + user.UserId, + user.Email, + user.FirstName, + user.LastName, + user.CreatedAt, + user.IsOrganisation, + OrganisationId + ); + } } }