-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Organizations allow users to group SFDC orgs so that they can work on groups of orgs at one time in isolation. This feature is most useful for consultants working across many separate groups of orgs, but is also useful to separate out production from other sandboxes etc.. With our new authentication provider, it appears like we may not be able to support multiple accounts with the same email address which was the catalyst for this feature. resolves #1018
- Loading branch information
Showing
47 changed files
with
1,967 additions
and
180 deletions.
There are no files selected for viewing
86 changes: 86 additions & 0 deletions
86
apps/api/src/app/controllers/jetstream-organizations.controller.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { z } from 'zod'; | ||
import * as jetstreamOrganizationsDb from '../db/organization.db'; | ||
import { UserFacingError } from '../utils/error-handler'; | ||
import { sendJson } from '../utils/response.handlers'; | ||
import { createRoute } from '../utils/route.utils'; | ||
|
||
export const routeDefinition = { | ||
getOrganizations: { | ||
controllerFn: () => getOrganizations, | ||
validators: { | ||
hasSourceOrg: false, | ||
}, | ||
}, | ||
createOrganization: { | ||
controllerFn: () => createOrganization, | ||
validators: { | ||
body: z.object({ | ||
name: z.string(), | ||
description: z.string().optional(), | ||
}), | ||
hasSourceOrg: false, | ||
}, | ||
}, | ||
updateOrganization: { | ||
controllerFn: () => updateOrganization, | ||
validators: { | ||
params: z.object({ | ||
id: z.string().uuid(), | ||
}), | ||
body: z.object({ | ||
name: z.string(), | ||
description: z.string().optional(), | ||
}), | ||
hasSourceOrg: false, | ||
}, | ||
}, | ||
deleteOrganization: { | ||
controllerFn: () => deleteOrganization, | ||
validators: { | ||
params: z.object({ | ||
id: z.string().uuid(), | ||
}), | ||
hasSourceOrg: false, | ||
}, | ||
}, | ||
}; | ||
|
||
const getOrganizations = createRoute(routeDefinition.getOrganizations.validators, async ({ user, query }, req, res, next) => { | ||
try { | ||
const organizations = await jetstreamOrganizationsDb.findByUserId({ userId: user.id }); | ||
|
||
sendJson(res, organizations); | ||
} catch (ex) { | ||
next(new UserFacingError(ex)); | ||
} | ||
}); | ||
|
||
const createOrganization = createRoute(routeDefinition.createOrganization.validators, async ({ user, body }, req, res, next) => { | ||
try { | ||
const organization = await jetstreamOrganizationsDb.create(user.id, body); | ||
|
||
sendJson(res, organization); | ||
} catch (ex) { | ||
next(new UserFacingError(ex)); | ||
} | ||
}); | ||
|
||
const updateOrganization = createRoute(routeDefinition.updateOrganization.validators, async ({ body, params, user }, req, res, next) => { | ||
try { | ||
const organization = await jetstreamOrganizationsDb.update(user.id, params.id, body); | ||
|
||
sendJson(res, organization, 201); | ||
} catch (ex) { | ||
next(new UserFacingError(ex)); | ||
} | ||
}); | ||
|
||
const deleteOrganization = createRoute(routeDefinition.deleteOrganization.validators, async ({ params, user }, req, res, next) => { | ||
try { | ||
await jetstreamOrganizationsDb.deleteOrganization(user.id, params.id); | ||
|
||
sendJson(res, undefined, 204); | ||
} catch (ex) { | ||
next(new UserFacingError(ex)); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* eslint-disable @typescript-eslint/no-non-null-assertion */ | ||
import { prisma } from '@jetstream/api-config'; | ||
import { Maybe } from '@jetstream/types'; | ||
import { Prisma } from '@prisma/client'; | ||
import { findIdByUserId } from './user.db'; | ||
|
||
const SELECT = Prisma.validator<Prisma.JetstreamOrganizationSelect>()({ | ||
id: true, | ||
orgs: { | ||
select: { uniqueId: true }, | ||
}, | ||
name: true, | ||
description: true, | ||
createdAt: true, | ||
updatedAt: true, | ||
}); | ||
|
||
export const findByUserId = async ({ userId }: { userId: string }) => { | ||
return await prisma.jetstreamOrganization.findMany({ where: { user: { userId } }, select: SELECT }); | ||
}; | ||
|
||
export const findById = async ({ id, userId }: { id: string; userId: string }) => { | ||
return await prisma.jetstreamOrganization.findFirstOrThrow({ where: { id, user: { userId } }, select: SELECT }); | ||
}; | ||
|
||
export const create = async ( | ||
userId: string, | ||
payload: { | ||
name: string; | ||
description?: Maybe<string>; | ||
} | ||
) => { | ||
const userActualId = await findIdByUserId({ userId }); | ||
return await prisma.jetstreamOrganization.create({ | ||
select: SELECT, | ||
data: { | ||
userId: userActualId, | ||
name: payload.name.trim(), | ||
description: payload.description?.trim(), | ||
}, | ||
}); | ||
}; | ||
|
||
export const update = async ( | ||
userId, | ||
id, | ||
payload: { | ||
name: string; | ||
description?: Maybe<string>; | ||
} | ||
) => { | ||
return await prisma.jetstreamOrganization.update({ | ||
select: SELECT, | ||
where: { user: { userId }, id }, | ||
data: { | ||
name: payload.name.trim(), | ||
description: payload.description?.trim() ?? null, | ||
}, | ||
}); | ||
}; | ||
|
||
export const deleteOrganization = async (userId, id) => { | ||
return await prisma.jetstreamOrganization.delete({ | ||
select: SELECT, | ||
where: { user: { userId }, id }, | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.