mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-10-08 14:28:20 +00:00
183 lines
4.7 KiB
TypeScript
183 lines
4.7 KiB
TypeScript
import { z } from "zod";
|
|
|
|
import {
|
|
IncidentContactsSchema,
|
|
OrganizationsSchema,
|
|
OrgMembershipsSchema,
|
|
UsersSchema
|
|
} from "@app/db/schemas";
|
|
import { verifyAuth } from "@app/server/plugins/auth/verify-auth";
|
|
import { AuthMode } from "@app/services/auth/auth-type";
|
|
|
|
export const registerOrgRouter = async (server: FastifyZodProvider) => {
|
|
server.route({
|
|
method: "GET",
|
|
url: "/",
|
|
schema: {
|
|
response: {
|
|
200: z.object({
|
|
organizations: OrganizationsSchema.array()
|
|
})
|
|
}
|
|
},
|
|
onRequest: verifyAuth([AuthMode.JWT]),
|
|
handler: async (req) => {
|
|
const organizations = await server.services.org.findAllOrganizationOfUser(req.auth.userId);
|
|
return { organizations };
|
|
}
|
|
});
|
|
|
|
server.route({
|
|
method: "GET",
|
|
url: "/:organizationId",
|
|
schema: {
|
|
params: z.object({
|
|
organizationId: z.string().trim()
|
|
}),
|
|
response: {
|
|
200: z.object({
|
|
organization: OrganizationsSchema
|
|
})
|
|
}
|
|
},
|
|
onRequest: verifyAuth([AuthMode.JWT]),
|
|
handler: async (req) => {
|
|
const organization = await server.services.org.findOrganizationById(
|
|
req.auth.userId,
|
|
req.params.organizationId
|
|
);
|
|
return { organization };
|
|
}
|
|
});
|
|
|
|
server.route({
|
|
method: "GET",
|
|
url: "/:organizationId/users",
|
|
schema: {
|
|
params: z.object({
|
|
organizationId: z.string().trim()
|
|
}),
|
|
response: {
|
|
200: z.object({
|
|
users: OrgMembershipsSchema.merge(
|
|
z.object({
|
|
user: UsersSchema.pick({
|
|
email: true,
|
|
firstName: true,
|
|
lastName: true,
|
|
id: true
|
|
}).merge(z.object({ publicKey: z.string().nullable() }))
|
|
})
|
|
)
|
|
.omit({ createdAt: true, updatedAt: true })
|
|
.array()
|
|
})
|
|
}
|
|
},
|
|
onRequest: verifyAuth([AuthMode.JWT]),
|
|
handler: async (req) => {
|
|
const users = await server.services.org.findAllOrgMembers(
|
|
req.auth.userId,
|
|
req.params.organizationId
|
|
);
|
|
return { users };
|
|
}
|
|
});
|
|
|
|
// TODO(akhilmhdh-pg): missing my-workspace list
|
|
|
|
server.route({
|
|
method: "PATCH",
|
|
url: "/:organizationId/name",
|
|
schema: {
|
|
params: z.object({ organizationId: z.string().trim() }),
|
|
body: z.object({ name: z.string().trim() }),
|
|
response: {
|
|
200: z.object({
|
|
message: z.string(),
|
|
organization: OrganizationsSchema
|
|
})
|
|
}
|
|
},
|
|
onRequest: verifyAuth([AuthMode.JWT]),
|
|
handler: async (req) => {
|
|
const organization = await server.services.org.updateOrgName(
|
|
req.auth.userId,
|
|
req.params.organizationId,
|
|
req.body.name
|
|
);
|
|
return {
|
|
message: "Successfully changed organization name",
|
|
organization
|
|
};
|
|
}
|
|
});
|
|
|
|
server.route({
|
|
method: "GET",
|
|
url: "/:organizationId/incidentContactOrg",
|
|
schema: {
|
|
params: z.object({ organizationId: z.string().trim() }),
|
|
response: {
|
|
200: z.object({
|
|
incidentContactsOrg: IncidentContactsSchema.array()
|
|
})
|
|
}
|
|
},
|
|
onRequest: verifyAuth([AuthMode.JWT]),
|
|
handler: async (req) => {
|
|
const incidentContactsOrg = await req.server.services.org.findIncidentContacts(
|
|
req.auth.userId,
|
|
req.params.organizationId
|
|
);
|
|
return { incidentContactsOrg };
|
|
}
|
|
});
|
|
|
|
server.route({
|
|
method: "POST",
|
|
url: "/:organizationId/incidentContactOrg",
|
|
schema: {
|
|
params: z.object({ organizationId: z.string().trim() }),
|
|
body: z.object({ email: z.string().email().trim() }),
|
|
response: {
|
|
200: z.object({
|
|
incidentContactsOrg: IncidentContactsSchema
|
|
})
|
|
}
|
|
},
|
|
onRequest: verifyAuth([AuthMode.JWT]),
|
|
handler: async (req) => {
|
|
const incidentContactsOrg = await req.server.services.org.createIncidentContact(
|
|
req.auth.userId,
|
|
req.params.organizationId,
|
|
req.body.email
|
|
);
|
|
return { incidentContactsOrg };
|
|
}
|
|
});
|
|
|
|
server.route({
|
|
method: "DELETE",
|
|
url: "/:organizationId/incidentContactOrg/:incidentContactId",
|
|
schema: {
|
|
// TODO(akhilmhdh-pg): change accept id instead of email
|
|
params: z.object({ organizationId: z.string().trim(), incidentContactId: z.string().trim() }),
|
|
response: {
|
|
200: z.object({
|
|
incidentContactsOrg: IncidentContactsSchema
|
|
})
|
|
}
|
|
},
|
|
onRequest: verifyAuth([AuthMode.JWT]),
|
|
handler: async (req) => {
|
|
const incidentContactsOrg = await req.server.services.org.deleteIncidentContact(
|
|
req.auth.userId,
|
|
req.params.organizationId,
|
|
req.params.incidentContactId
|
|
);
|
|
return { incidentContactsOrg };
|
|
}
|
|
});
|
|
};
|