-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
959e2b7
commit a1ebed0
Showing
7 changed files
with
193 additions
and
8 deletions.
There are no files selected for viewing
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,21 @@ | ||
exports.up = async function (knex) { | ||
await knex.schema.createTable("sentry_teams", (table) => { | ||
table.uuid("id").primary().defaultTo(knex.raw("gen_random_uuid()")); // UUID pour l'identifiant unique | ||
table.string("sentry_id").notNullable().unique(); // Identifiant de team Sentry | ||
table.uuid("startup_id").nullable(); // UUID lié à la table startups | ||
table.string("name").notNullable(); // Nom de l'équipe | ||
|
||
// Clé étrangère vers la table startups | ||
table | ||
.foreign("startup_id") | ||
.references("uuid") | ||
.inTable("startups") | ||
.onDelete("CASCADE"); | ||
|
||
table.timestamps(true, true); // Champs created_at et updated_at | ||
}); | ||
}; | ||
|
||
exports.down = async function (knex) { | ||
await knex.schema.dropTableIfExists("sentry_teams"); | ||
}; |
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,56 @@ | ||
import { redirect } from "next/navigation"; | ||
import { getServerSession } from "next-auth/next"; | ||
|
||
import AccountDetails from "@/components/Service/AccountDetails"; | ||
import SentryServiceForm from "@/components/Service/SentryServiceForm"; | ||
import { getServiceAccount } from "@/lib/kysely/queries/services"; | ||
import { sentryServiceInfoToModel } from "@/models/mapper/sentryMapper"; | ||
import { sentryUserSchemaType } from "@/models/sentry"; | ||
import { SERVICES } from "@/models/services"; | ||
import config from "@/server/config"; | ||
import { authOptions } from "@/utils/authoptions"; | ||
|
||
const buildLinkToSentryTeam = ( | ||
team: sentryUserSchemaType["metadata"]["teams"][0] | ||
) => { | ||
return team.name ? ( | ||
<a href={`${config.SENTRY_WEBSITE_URL}/${team.slug}`} target="_blank"> | ||
{team.name} | ||
</a> | ||
) : ( | ||
team.name | ||
); | ||
}; | ||
|
||
export default async function SentryPage() { | ||
const session = await getServerSession(authOptions); | ||
if (!session) { | ||
redirect("/login"); | ||
} | ||
|
||
const rawAccount = await getServiceAccount( | ||
session.user.uuid, | ||
SERVICES.SENTRY | ||
); | ||
const service_account = rawAccount | ||
? sentryServiceInfoToModel(rawAccount) | ||
: undefined; | ||
|
||
return ( | ||
<> | ||
<h1>Compte Sentry</h1> | ||
{service_account ? ( | ||
<AccountDetails | ||
account={service_account} | ||
data={service_account.metadata.teams.map((team) => [ | ||
buildLinkToSentryTeam(team), | ||
team.role, | ||
])} | ||
headers={["nom", "niveau d'accès"]} | ||
/> | ||
) : ( | ||
<SentryServiceForm /> | ||
)} | ||
</> | ||
); | ||
} |
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,11 @@ | ||
import { db } from "@/lib/kysely"; | ||
import { SERVICES } from "@/models/services"; | ||
|
||
export async function getServiceAccount(userId: string, service: SERVICES) { | ||
return db | ||
.selectFrom("service_accounts") | ||
.selectAll() | ||
.where("user_id", "=", userId) | ||
.where("account_type", "=", service) | ||
.executeTakeFirst(); | ||
} |
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,43 @@ | ||
import pAll from "p-all"; | ||
import PgBoss from "pg-boss"; | ||
|
||
import { db } from "@/lib/kysely"; | ||
import { CreateSentryAccountDataSchemaType } from "@/models/jobs/services"; | ||
import { sentryMetadataToModel } from "@/models/mapper/sentryMapper"; | ||
import { ACCOUNT_SERVICE_STATUS, SERVICES } from "@/models/services"; | ||
import { sentryClient } from "@/server/config/sentry.config"; | ||
import { decryptPassword } from "@/server/controllers/utils"; | ||
|
||
export const createSentryServiceAccountTopic = "create-sentry-service-account"; | ||
|
||
export async function createSentryServiceAccount( | ||
job: PgBoss.Job<CreateSentryAccountDataSchemaType> | ||
) { | ||
console.log( | ||
`Create sentry service account for ${job.data.login}`, | ||
job.id, | ||
job.name | ||
); | ||
|
||
// throw new Error("Account could not be created"); | ||
const userLogin = job.data.email; | ||
|
||
await sentryClient.createUser({ | ||
email: job.data.email, | ||
password: decryptPassword(job.data.password), | ||
userLogin, | ||
alias: job.data.email, | ||
teams: [], | ||
}); | ||
|
||
const result = await db | ||
.updateTable("service_accounts") | ||
.set({ | ||
service_user_id: userLogin, | ||
status: ACCOUNT_SERVICE_STATUS.ACCOUNT_FOUND, | ||
metadata: JSON.stringify(metadata), | ||
}) | ||
.executeTakeFirstOrThrow(); | ||
|
||
console.log(`the sentry account has been created for ${userLogin}`); | ||
} |