Fix authContext missing in ALS WorkspaceContext for global datasource (#15810)

Fixes https://github.com/twentyhq/twenty/issues/15809

## Context
We recently introduced a global datasource now consuming workspace
context from ALS store however the authContext was missing (only the
workspaceId was there) which "broke" event emission, now missing the
workspaceMemberId

This PR adds the missing authContext so we can access from anywhere in
the datasource. We are still passing it as a parameters on repository
level for legacy but in theory we should be able to remove it from
everywhere and consume the context

<img width="929" height="253" alt="Screenshot 2025-11-13 at 18 58 16"
src="https://github.com/user-attachments/assets/3e04f264-95e6-4831-94f3-fc01603f19bd"
/>
This commit is contained in:
Weiko
2025-11-14 10:59:15 +01:00
committed by GitHub
parent bff079d3b5
commit 4dd6edb35a
4 changed files with 40 additions and 27 deletions
@@ -2,11 +2,13 @@ import { AsyncLocalStorage } from 'async_hooks';
import { type ObjectsPermissionsByRoleId } from 'twenty-shared/types';
import { type WorkspaceAuthContext } from 'src/engine/api/common/interfaces/workspace-auth-context.interface';
import { type FeatureFlagKey } from 'src/engine/core-modules/feature-flag/enums/feature-flag-key.enum';
import { type ObjectMetadataMaps } from 'src/engine/metadata-modules/types/object-metadata-maps';
export type WorkspaceContextForStorage = {
workspaceId: string;
export type WorkspaceContext = {
authContext: WorkspaceAuthContext;
objectMetadataMaps: ObjectMetadataMaps;
metadataVersion: number;
featureFlagsMap: Record<FeatureFlagKey, boolean>;
@@ -14,9 +16,9 @@ export type WorkspaceContextForStorage = {
};
export const workspaceContextStorage =
new AsyncLocalStorage<WorkspaceContextForStorage>();
new AsyncLocalStorage<WorkspaceContext>();
export const getWorkspaceContext = (): WorkspaceContextForStorage => {
export const getWorkspaceContext = (): WorkspaceContext => {
const context = workspaceContextStorage.getStore();
if (!context) {
@@ -29,14 +31,12 @@ export const getWorkspaceContext = (): WorkspaceContextForStorage => {
};
export const withWorkspaceContext = <T>(
context: WorkspaceContextForStorage,
context: WorkspaceContext,
fn: () => T | Promise<T>,
): T | Promise<T> => {
return workspaceContextStorage.run(context, fn);
};
export const setWorkspaceContext = (
context: WorkspaceContextForStorage,
): void => {
export const setWorkspaceContext = (context: WorkspaceContext): void => {
workspaceContextStorage.enterWith(context);
};