Skip to content

Commit

Permalink
Merge branch 'dev' into feat/dashboard-v2-filters-order
Browse files Browse the repository at this point in the history
  • Loading branch information
kornifex authored Sep 4, 2023
2 parents 0364ebf + b85f598 commit 4cb8cc2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
8 changes: 6 additions & 2 deletions back/src/companies/resolvers/CompanyPrivate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,19 @@ const companyPrivateResolvers: CompanyPrivateResolvers = {
return getCompanyUsers(parent.orgId, context.dataloaders, userId);
},
userRole: async (parent, _, context) => {
if (parent.userRole) {
return parent.userRole;
}

const userId = context.user!.id;
const role = await getUserRole(userId, parent.orgId);
// type casting is necessary here as long as we
// do not expose READER and DRIVER role in the API
return role as UserRole;
},
userPermissions: async (parent, _, context) => {
const userId = context.user!.id;
const role = await getUserRole(userId, parent.orgId);
const role =
parent.userRole ?? (await getUserRole(context.user!.id, parent.orgId));
return role ? grants[role].map(toGraphQLPermission) : [];
},
transporterReceipt: parent => {
Expand Down
17 changes: 14 additions & 3 deletions back/src/users/resolvers/User.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { convertUrls } from "../../companies/database";
import { UserResolvers, CompanyPrivate } from "../../generated/graphql/types";
import { getUserCompanies } from "../database";
import {
UserResolvers,
CompanyPrivate,
UserRole
} from "../../generated/graphql/types";
import { nafCodes } from "../../common/constants/NAF";
import prisma from "../../prisma";

Expand All @@ -9,7 +12,15 @@ const userResolvers: UserResolvers = {
// Information from TD and s3ic are merged in a separate resolver (CompanyPrivate.installation)
// to make up an instance of CompanyPrivate
companies: async parent => {
const companies = await getUserCompanies(parent.id);
const companyAssociations = await prisma.companyAssociation.findMany({
where: { userId: parent.id },
include: { company: true }
});
const companies = companyAssociations.map(association => ({
...association.company,
userRole: association.role as UserRole
}));

return companies.map(async company => {
const companyPrivate: CompanyPrivate = convertUrls(company);

Expand Down

0 comments on commit 4cb8cc2

Please sign in to comment.