Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

feat(teams): client methods, queries, and mutations (#2798)

dev-rb0f1cd0f49b62
diff --git a/js/app/packages/queries/team/index.ts b/js/app/packages/queries/team/index.ts
new file mode 100644
index 0000000000..694af39107
--- /dev/null
+++ b/js/app/packages/queries/team/index.ts
@@ -0,0 +1,30 @@
+export { teamKeys } from './keys';
+
+export {
+ useUserTeamsQuery,
+ useTeamQuery,
+ useCreateTeamMutation,
+ usePatchTeamMutation,
+ useDeleteTeamMutation,
+ invalidateUserTeams,
+ invalidateTeam,
+} from './teams';
+
+export {
+ useTeamInvitesQuery,
+ useInviteToTeamMutation,
+ useDeleteTeamInviteMutation,
+ invalidateTeamInvites,
+} from './invites';
+
+export {
+ useUserInvitesQuery,
+ useJoinTeamMutation,
+ useRejectInvitationMutation,
+ invalidateUserInvites,
+} from './invitations';
+
+export {
+ usePatchTeamUserTierMutation,
+ useRemoveUserFromTeamMutation,
+} from './members';
diff --git a/js/app/packages/queries/team/invitations.ts b/js/app/packages/queries/team/invitations.ts
new file mode 100644
index 0000000000..39787682ed
--- /dev/null
+++ b/js/app/packages/queries/team/invitations.ts
@@ -0,0 +1,161 @@
+import { toast } from '@core/component/Toast/Toast';
+import { throwOnErr } from '@core/util/maybeResult';
+import { authServiceClient } from '@service-auth/client';
+import type { TeamInvitesResponse } from '@service-auth/generated/schemas/teamInvitesResponse';
+import { useMutation, useQuery } from '@tanstack/solid-query';
+
+import { queryClient } from '../client';
+import { type MutationCallbacks, withCallbacks } from '../utils';
+
+import { teamKeys } from './keys';
+import { invalidateUserTeams } from './teams';
+
+export function useUserInvitesQuery() {
+ return useQuery(() => ({
+ queryKey: teamKeys.userInvites.queryKey,
+ queryFn: async () =>
+ await throwOnErr(() => authServiceClient.getUserInvites()),
+ }));
+}
+
+export function invalidateUserInvites() {
+ return queryClient.invalidateQueries({
+ queryKey: teamKeys.userInvites.queryKey,
+ });
+}
+
+type JoinTeamArgs = { teamInviteId: string };
+type JoinTeamContext = { previousInvites: TeamInvitesResponse | undefined };
+type JoinTeamCallbacks = MutationCallbacks<
+ void,
+ Error,
+ JoinTeamArgs,
+ JoinTeamContext
+>;
+
+export function useJoinTeamMutation(callbacks?: JoinTeamCallbacks) {
+ return useMutation(() => ({
+ mutationFn: async ({ teamInviteId }: JoinTeamArgs) => {
+ await throwOnErr(() => authServiceClient.joinTeam(teamInviteId));
+ },
+
+ ...withCallbacks<void, Error, JoinTeamArgs, JoinTeamContext>(
+ {
+ onMutate: async ({ teamInviteId }) => {
+ await queryClient.cancelQueries({
+ queryKey: teamKeys.userInvites.queryKey,
+ });
+
+ const previousInvites = queryClient.getQueryData<TeamInvitesResponse>(
+ teamKeys.userInvites.queryKey
+ );
+
+ queryClient.setQueryData<TeamInvitesResponse>(
+ teamKeys.userInvites.queryKey,
+ (old) =>
+ old
+ ? {
+ invites: old.invites.filter(
+ (invite) => invite.id !== teamInviteId
+ ),
+ }
+ : undefined
+ );
+
+ return { previousInvites };
+ },
+
+ onSuccess: () => {
+ invalidateUserTeams();
+ invalidateUserInvites();
+ toast.success('Joined team');
+ },
+
+ onError: (error, _vars, context) => {
+ console.error('Failed to join team', error);
+ toast.failure('Failed to join team');
+
+ if (context?.previousInvites) {
+ queryClient.setQueryData(
+ teamKeys.userInvites.queryKey,
+ context.previousInvites
+ );
+ }
+ },
+ },
+ callbacks
+ ),
+ }));
+}
+
+type RejectInvitationArgs = { teamInviteId: string };
+type RejectInvitationContext = {
+ previousInvites: TeamInvitesResponse | undefined;
+};
+type RejectInvitationCallbacks = MutationCallbacks<
+ void,
+ Error,
+ RejectInvitationArgs,
+ RejectInvitationContext
+>;
+
+export function useRejectInvitationMutation(
+ callbacks?: RejectInvitationCallbacks
+) {
+ return useMutation(() => ({
+ mutationFn: async ({ teamInviteId }: RejectInvitationArgs) => {
+ await throwOnErr(() => authServiceClient.rejectInvitation(teamInviteId));
+ },
+
+ ...withCallbacks<
+ void,
+ Error,
+ RejectInvitationArgs,
+ RejectInvitationContext
+ >(
+ {
+ onMutate: async ({ teamInviteId }) => {
+ await queryClient.cancelQueries({
+ queryKey: teamKeys.userInvites.queryKey,
+ });
+
+ const previousInvites = queryClient.getQueryData<TeamInvitesResponse>(
+ teamKeys.userInvites.queryKey
+ );
+
+ queryClient.setQueryData<TeamInvitesResponse>(
+ teamKeys.userInvites.queryKey,
+ (old) =>
+ old
+ ? {
+ invites: old.invites.filter(
+ (invite) => invite.id !== teamInviteId
+ ),
+ }
+ : undefined
+ );
+
+ return { previousInvites };
+ },
+
+ onSuccess: () => {
+ invalidateUserInvites();
+ toast.success('Invitation declined');
+ },
+
+ onError: (error, _vars, context) => {
+ console.error('Failed to reject invitation', error);
+ toast.failure('Failed to reject invitation');
+
+ if (context?.previousInvites) {
+ queryClient.setQueryData(
+ teamKeys.userInvites.queryKey,
+ context.previousInvites
+ );
+ }
+ },
+ },
+ callbacks
+ ),
+ }));
+}
diff --git a/js/app/packages/queries/team/invites.ts b/js/app/packages/queries/team/invites.ts
new file mode 100644
index 0000000000..38814177da
--- /dev/null
+++ b/js/app/packages/queries/team/invites.ts
@@ -0,0 +1,123 @@
+import { toast } from '@core/component/Toast/Toast';
+import { throwOnErr } from '@core/util/maybeResult';
+import { authServiceClient } from '@service-auth/client';
+import type { InviteToTeamRequest } from '@service-auth/generated/schemas/inviteToTeamRequest';
+import type { TeamInvitesResponse } from '@service-auth/generated/schemas/teamInvitesResponse';
+import { useMutation, useQuery } from '@tanstack/solid-query';
+import type { Accessor } from 'solid-js';
+
+import { queryClient } from '../client';
+import { type MutationCallbacks, withCallbacks } from '../utils';
+
+import { teamKeys } from './keys';
+
+export function useTeamInvitesQuery(teamId: Accessor<string>) {
+ return useQuery(() => ({
+ queryKey: teamKeys.invites(teamId()).queryKey,
+ queryFn: async () =>
+ await throwOnErr(() => authServiceClient.getTeamInvites(teamId())),
+ enabled: !!teamId(),
+ }));
+}
+
+export function invalidateTeamInvites(teamId: string) {
+ return queryClient.invalidateQueries({
+ queryKey: teamKeys.invites(teamId).queryKey,
+ });
+}
+
+type InviteToTeamArgs = { teamId: string; request: InviteToTeamRequest };
+type InviteToTeamCallbacks = MutationCallbacks<void, Error, InviteToTeamArgs>;
+
+export function useInviteToTeamMutation(callbacks?: InviteToTeamCallbacks) {
+ return useMutation(() => ({
+ mutationFn: async ({ teamId, request }: InviteToTeamArgs) => {
+ await throwOnErr(() => authServiceClient.inviteToTeam(teamId, request));
+ },
+
+ ...withCallbacks<void, Error, InviteToTeamArgs>(
+ {
+ onSuccess: (_data, { teamId }) => {
+ invalidateTeamInvites(teamId);
+ toast.success('Invitation sent');
+ },
+
+ onError: (error) => {
+ console.error('Failed to invite to team', error);
+ toast.failure('Failed to send invitation');
+ },
+ },
+ callbacks
+ ),
+ }));
+}
+
+type DeleteTeamInviteArgs = { teamId: string; teamInviteId: string };
+type DeleteTeamInviteContext = {
+ previousInvites: TeamInvitesResponse | undefined;
+};
+type DeleteTeamInviteCallbacks = MutationCallbacks<
+ void,
+ Error,
+ DeleteTeamInviteArgs,
+ DeleteTeamInviteContext
+>;
+
+export function useDeleteTeamInviteMutation(
+ callbacks?: DeleteTeamInviteCallbacks
+) {
+ return useMutation(() => ({
+ mutationFn: async ({ teamId, teamInviteId }: DeleteTeamInviteArgs) => {
+ await throwOnErr(() =>
+ authServiceClient.deleteTeamInvite(teamId, teamInviteId)
+ );
+ },
+
+ ...withCallbacks<
+ void,
+ Error,
+ DeleteTeamInviteArgs,
+ DeleteTeamInviteContext
+ >(
+ {
+ onMutate: async ({ teamId, teamInviteId }) => {
+ const queryKey = teamKeys.invites(teamId).queryKey;
+ await queryClient.cancelQueries({ queryKey });
+
+ const previousInvites =
+ queryClient.getQueryData<TeamInvitesResponse>(queryKey);
+
+ queryClient.setQueryData<TeamInvitesResponse>(queryKey, (old) =>
+ old
+ ? {
+ invites: old.invites.filter(
+ (invite) => invite.id !== teamInviteId
+ ),
+ }
+ : undefined
+ );
+
+ return { previousInvites };
+ },
+
+ onSuccess: (_data, { teamId }) => {
+ invalidateTeamInvites(teamId);
+ toast.success('Invitation cancelled');
+ },
+
+ onError: (error, { teamId }, context) => {
+ console.error('Failed to delete team invite', error);
+ toast.failure('Failed to cancel invitation');
+
+ if (context?.previousInvites) {
+ queryClient.setQueryData(
+ teamKeys.invites(teamId).queryKey,
+ context.previousInvites
+ );
+ }
+ },
+ },
+ callbacks
+ ),
+ }));
+}
diff --git a/js/app/packages/queries/team/keys.ts b/js/app/packages/queries/team/keys.ts
new file mode 100644
index 0000000000..ff32786a6b
--- /dev/null
+++ b/js/app/packages/queries/team/keys.ts
@@ -0,0 +1,8 @@
+import { createQueryKeys } from '@lukemorales/query-key-factory';
+
+export const teamKeys = createQueryKeys('team', {
+ userTeams: null,
+ userInvites: null,
+ detail: (teamId: string) => [teamId],
+ invites: (teamId: string) => [teamId],
+});
diff --git a/js/app/packages/queries/team/members.ts b/js/app/packages/queries/team/members.ts
new file mode 100644
index 0000000000..7bfa1c1147
--- /dev/null
+++ b/js/app/packages/queries/team/members.ts
@@ -0,0 +1,118 @@
+import { toast } from '@core/component/Toast/Toast';
+import { throwOnErr } from '@core/util/maybeResult';
+import { authServiceClient } from '@service-auth/client';
+import type { PatchTeamUserTierRequest } from '@service-auth/generated/schemas/patchTeamUserTierRequest';
+import type { TeamWithMembers } from '@service-auth/generated/schemas/teamWithMembers';
+import { useMutation } from '@tanstack/solid-query';
+
+import { queryClient } from '../client';
+import { type MutationCallbacks, withCallbacks } from '../utils';
+
+import { teamKeys } from './keys';
+import { invalidateTeam } from './teams';
+
+type PatchTeamUserTierArgs = {
+ teamId: string;
+ request: PatchTeamUserTierRequest;
+};
+type PatchTeamUserTierCallbacks = MutationCallbacks<
+ void,
+ Error,
+ PatchTeamUserTierArgs
+>;
+
+export function usePatchTeamUserTierMutation(
+ callbacks?: PatchTeamUserTierCallbacks
+) {
+ return useMutation(() => ({
+ mutationFn: async ({ teamId, request }: PatchTeamUserTierArgs) => {
+ await throwOnErr(() =>
+ authServiceClient.patchTeamUserTier(teamId, request)
+ );
+ },
+
+ ...withCallbacks<void, Error, PatchTeamUserTierArgs>(
+ {
+ onSuccess: (_data, { teamId }) => {
+ invalidateTeam(teamId);
+ toast.success('Member tier updated');
+ },
+
+ onError: (error) => {
+ console.error('Failed to update team member tier', error);
+ toast.failure('Failed to update team member tier');
+ },
+ },
+ callbacks
+ ),
+ }));
+}
+
+type RemoveUserFromTeamArgs = { teamId: string; userId: string };
+type RemoveUserFromTeamContext = { previousTeam: TeamWithMembers | undefined };
+type RemoveUserFromTeamCallbacks = MutationCallbacks<
+ void,
+ Error,
+ RemoveUserFromTeamArgs,
+ RemoveUserFromTeamContext
+>;
+
+export function useRemoveUserFromTeamMutation(
+ callbacks?: RemoveUserFromTeamCallbacks
+) {
+ return useMutation(() => ({
+ mutationFn: async ({ teamId, userId }: RemoveUserFromTeamArgs) => {
+ await throwOnErr(() =>
+ authServiceClient.removeUserFromTeam(teamId, userId)
+ );
+ },
+
+ ...withCallbacks<
+ void,
+ Error,
+ RemoveUserFromTeamArgs,
+ RemoveUserFromTeamContext
+ >(
+ {
+ onMutate: async ({ teamId, userId }) => {
+ const queryKey = teamKeys.detail(teamId).queryKey;
+ await queryClient.cancelQueries({ queryKey });
+
+ const previousTeam =
+ queryClient.getQueryData<TeamWithMembers>(queryKey);
+
+ queryClient.setQueryData<TeamWithMembers>(queryKey, (old) =>
+ old
+ ? {
+ ...old,
+ members: old.members.filter(
+ (member) => member.user_id !== userId
+ ),
+ }
+ : undefined
+ );
+
+ return { previousTeam };
+ },
+
+ onSuccess: (_data, { teamId }) => {
+ invalidateTeam(teamId);
+ toast.success('Member removed');
+ },
+
+ onError: (error, { teamId }, context) => {
+ console.error('Failed to remove user from team', error);
+ toast.failure('Failed to remove team member');
+
+ if (context?.previousTeam) {
+ queryClient.setQueryData(
+ teamKeys.detail(teamId).queryKey,
+ context.previousTeam
+ );
+ }
+ },
+ },
+ callbacks
+ ),
+ }));
+}
diff --git a/js/app/packages/queries/team/teams.ts b/js/app/packages/queries/team/teams.ts
new file mode 100644
index 0000000000..4c46265a0f
--- /dev/null
+++ b/js/app/packages/queries/team/teams.ts
@@ -0,0 +1,149 @@
+import { toast } from '@core/component/Toast/Toast';
+import { throwOnErr } from '@core/util/maybeResult';
+import { authServiceClient } from '@service-auth/client';
+import type { CreateTeamRequest } from '@service-auth/generated/schemas/createTeamRequest';
+import type { PatchTeamRequest } from '@service-auth/generated/schemas/patchTeamRequest';
+import type { Team } from '@service-auth/generated/schemas/team';
+import { useMutation, useQuery } from '@tanstack/solid-query';
+import type { Accessor } from 'solid-js';
+
+import { queryClient } from '../client';
+import { type MutationCallbacks, withCallbacks } from '../utils';
+
+import { teamKeys } from './keys';
+
+export function useUserTeamsQuery() {
+ return useQuery(() => ({
+ queryKey: teamKeys.userTeams.queryKey,
+ queryFn: async () =>
+ await throwOnErr(() => authServiceClient.getUserTeams()),
+ }));
+}
+
+export function useTeamQuery(teamId: Accessor<string>) {
+ return useQuery(() => ({
+ queryKey: teamKeys.detail(teamId()).queryKey,
+ queryFn: async () =>
+ await throwOnErr(() => authServiceClient.getTeam(teamId())),
+ enabled: !!teamId(),
+ }));
+}
+
+export function invalidateUserTeams() {
+ return queryClient.invalidateQueries({
+ queryKey: teamKeys.userTeams.queryKey,
+ });
+}
+
+export function invalidateTeam(teamId: string) {
+ return queryClient.invalidateQueries({
+ queryKey: teamKeys.detail(teamId).queryKey,
+ });
+}
+
+type CreateTeamArgs = CreateTeamRequest;
+type CreateTeamCallbacks = MutationCallbacks<Team, Error, CreateTeamArgs>;
+
+export function useCreateTeamMutation(callbacks?: CreateTeamCallbacks) {
+ return useMutation(() => ({
+ mutationFn: async (args: CreateTeamArgs) =>
+ await throwOnErr(() => authServiceClient.createTeam(args)),
+
+ ...withCallbacks<Team, Error, CreateTeamArgs>(
+ {
+ onSuccess: () => {
+ invalidateUserTeams();
+ toast.success('Team created');
+ },
+
+ onError: (error) => {
+ console.error('Failed to create team', error);
+ toast.failure('Failed to create team');
+ },
+ },
+ callbacks
+ ),
+ }));
+}
+
+type PatchTeamArgs = { teamId: string; request: PatchTeamRequest };
+type PatchTeamCallbacks = MutationCallbacks<void, Error, PatchTeamArgs>;
+
+export function usePatchTeamMutation(callbacks?: PatchTeamCallbacks) {
+ return useMutation(() => ({
+ mutationFn: async ({ teamId, request }: PatchTeamArgs) => {
+ await throwOnErr(() => authServiceClient.patchTeam(teamId, request));
+ },
+
+ ...withCallbacks<void, Error, PatchTeamArgs>(
+ {
+ onSuccess: (_data, { teamId }) => {
+ invalidateTeam(teamId);
+ invalidateUserTeams();
+ toast.success('Team updated');
+ },
+
+ onError: (error) => {
+ console.error('Failed to update team', error);
+ toast.failure('Failed to update team');
+ },
+ },
+ callbacks
+ ),
+ }));
+}
+
+type DeleteTeamArgs = { teamId: string };
+type DeleteTeamContext = { previousTeams: Team[] | undefined };
+type DeleteTeamCallbacks = MutationCallbacks<
+ void,
+ Error,
+ DeleteTeamArgs,
+ DeleteTeamContext
+>;
+
+export function useDeleteTeamMutation(callbacks?: DeleteTeamCallbacks) {
+ return useMutation(() => ({
+ mutationFn: async ({ teamId }: DeleteTeamArgs) => {
+ await throwOnErr(() => authServiceClient.deleteTeam(teamId));
+ },
+
+ ...withCallbacks<void, Error, DeleteTeamArgs, DeleteTeamContext>(
+ {
+ onMutate: async ({ teamId }) => {
+ await queryClient.cancelQueries({
+ queryKey: teamKeys.userTeams.queryKey,
+ });
+
+ const previousTeams = queryClient.getQueryData<Team[]>(
+ teamKeys.userTeams.queryKey
+ );
+
+ queryClient.setQueryData<Team[]>(teamKeys.userTeams.queryKey, (old) =>
+ old?.filter((team) => team.id !== teamId)
+ );
+
+ return { previousTeams };
+ },
+
+ onSuccess: () => {
+ invalidateUserTeams();
+ toast.success('Team deleted');
+ },
+
+ onError: (error, _args, context) => {
+ console.error('Failed to delete team', error);
+ toast.failure('Failed to delete team');
+
+ if (context?.previousTeams) {
+ queryClient.setQueryData(
+ teamKeys.userTeams.queryKey,
+ context.previousTeams
+ );
+ }
+ },
+ },
+ callbacks
+ ),
+ }));
+}
diff --git a/js/app/packages/service-clients/service-auth/client.ts b/js/app/packages/service-clients/service-auth/client.ts
index c3714aed1d..8f0150f726 100644
--- a/js/app/packages/service-clients/service-auth/client.ts
+++ b/js/app/packages/service-clients/service-auth/client.ts
@@ -40,6 +40,13 @@ import type { UserName } from './generated/schemas/userName';
import type { UserNames } from './generated/schemas/userNames';
import type { UserOrganizationResponse } from './generated/schemas/userOrganizationResponse';
import type { UserTokensResponse } from './generated/schemas/userTokensResponse';
+import type { CreateTeamRequest } from './generated/schemas/createTeamRequest';
+import type { InviteToTeamRequest } from './generated/schemas/inviteToTeamRequest';
+import type { PatchTeamRequest } from './generated/schemas/patchTeamRequest';
+import type { PatchTeamUserTierRequest } from './generated/schemas/patchTeamUserTierRequest';
+import type { Team } from './generated/schemas/team';
+import type { TeamInvitesResponse } from './generated/schemas/teamInvitesResponse';
+import type { TeamWithMembers } from './generated/schemas/teamWithMembers';
const authHost = SERVER_HOSTS['auth-service'];
@@ -576,6 +583,130 @@ export const authServiceClient = {
}
);
},
+
+ async getUserTeams() {
+ return mapOk(
+ await fetchWithAuth<Team[]>(`${authHost}/team/user`, { method: 'GET' }),
+ (result) => result
+ );
+ },
+
+ async getUserInvites() {
+ return mapOk(
+ await fetchWithAuth<TeamInvitesResponse>(
+ `${authHost}/team/user/invites`,
+ {
+ method: 'GET',
+ }
+ ),
+ (result) => result
+ );
+ },
+
+ async getTeam(teamId: string) {
+ return mapOk(
+ await fetchWithAuth<TeamWithMembers>(`${authHost}/team/${teamId}`, {
+ method: 'GET',
+ }),
+ (result) => result
+ );
+ },
+
+ async getTeamInvites(teamId: string) {
+ return mapOk(
+ await fetchWithAuth<TeamInvitesResponse>(
+ `${authHost}/team/${teamId}/invites`,
+ { method: 'GET' }
+ ),
+ (result) => result
+ );
+ },
+
+ async createTeam(args: CreateTeamRequest) {
+ return mapOk(
+ await fetchWithAuth<Team>(`${authHost}/team`, {
+ method: 'POST',
+ body: JSON.stringify(args),
+ }),
+ (result) => result
+ );
+ },
+
+ async joinTeam(teamInviteId: string) {
+ return mapOk(
+ await fetchWithAuth<{}>(`${authHost}/team/join/${teamInviteId}`, {
+ method: 'GET',
+ }),
+ () => undefined
+ );
+ },
+
+ async rejectInvitation(teamInviteId: string) {
+ return mapOk(
+ await fetchWithAuth<{}>(`${authHost}/team/join/${teamInviteId}`, {
+ method: 'DELETE',
+ }),
+ () => undefined
+ );
+ },
+
+ async patchTeam(teamId: string, args: PatchTeamRequest) {
+ return mapOk(
+ await fetchWithAuth<{}>(`${authHost}/team/${teamId}`, {
+ method: 'PATCH',
+ body: JSON.stringify(args),
+ }),
+ () => undefined
+ );
+ },
+
+ async patchTeamUserTier(teamId: string, args: PatchTeamUserTierRequest) {
+ return mapOk(
+ await fetchWithAuth<{}>(`${authHost}/team/${teamId}/tier`, {
+ method: 'PATCH',
+ body: JSON.stringify(args),
+ }),
+ () => undefined
+ );
+ },
+
+ async inviteToTeam(teamId: string, args: InviteToTeamRequest) {
+ return mapOk(
+ await fetchWithAuth<{}>(`${authHost}/team/${teamId}/invite`, {
+ method: 'POST',
+ body: JSON.stringify(args),
+ }),
+ () => undefined
+ );
+ },
+
+ async deleteTeamInvite(teamId: string, teamInviteId: string) {
+ return mapOk(
+ await fetchWithAuth<{}>(
+ `${authHost}/team/${teamId}/invite/${teamInviteId}`,
+ { method: 'DELETE' }
+ ),
+ () => undefined
+ );
+ },
+
+ async removeUserFromTeam(teamId: string, userId: string) {
+ return mapOk(
+ await fetchWithAuth<{}>(`${authHost}/team/${teamId}/remove/${userId}`, {
+ method: 'DELETE',
+ }),
+ () => undefined
+ );
+ },
+
+ async deleteTeam(teamId: string) {
+ return mapOk(
+ await fetchWithAuth<{}>(`${authHost}/team/${teamId}`, {
+ method: 'DELETE',
+ }),
+ () => undefined
+ );
+ },
};
registerClient('auth', authServiceClient);