-
Notifications
You must be signed in to change notification settings - Fork 739
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: enabled customization of project audit logs retention period
- Loading branch information
1 parent
9a66514
commit 11ea599
Showing
11 changed files
with
261 additions
and
7 deletions.
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
backend/src/db/migrations/20240625115447_configurable-audit-log-retention.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,19 @@ | ||
import { Knex } from "knex"; | ||
|
||
import { TableName } from "../schemas"; | ||
|
||
export async function up(knex: Knex): Promise<void> { | ||
if (!(await knex.schema.hasColumn(TableName.Project, "auditLogsRetentionDays"))) { | ||
await knex.schema.alterTable(TableName.Project, (tb) => { | ||
tb.integer("auditLogsRetentionDays").nullable(); | ||
}); | ||
} | ||
} | ||
|
||
export async function down(knex: Knex): Promise<void> { | ||
if (await knex.schema.hasColumn(TableName.Project, "auditLogsRetentionDays")) { | ||
await knex.schema.alterTable(TableName.Project, (t) => { | ||
t.dropColumn("auditLogsRetentionDays"); | ||
}); | ||
} | ||
} |
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
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
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
120 changes: 120 additions & 0 deletions
120
...gs/ProjectSettingsPage/components/AuditLogsRetentionSection/AuditLogsRetentionSection.tsx
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,120 @@ | ||
import { Controller, useForm } from "react-hook-form"; | ||
import { zodResolver } from "@hookform/resolvers/zod"; | ||
import { z } from "zod"; | ||
|
||
import { createNotification } from "@app/components/notifications"; | ||
import { Button, FormControl, Input, UpgradePlanModal } from "@app/components/v2"; | ||
import { useProjectPermission, useSubscription, useWorkspace } from "@app/context"; | ||
import { usePopUp } from "@app/hooks"; | ||
import { ProjectMembershipRole } from "@app/hooks/api/roles/types"; | ||
import { useUpdateWorkspaceAuditLogsRetention } from "@app/hooks/api/workspace/queries"; | ||
|
||
const formSchema = z.object({ | ||
auditLogsRetentionDays: z.coerce.number().min(0) | ||
}); | ||
|
||
type TForm = z.infer<typeof formSchema>; | ||
|
||
export const AuditLogsRetentionSection = () => { | ||
const { mutateAsync: updateAuditLogsRetention } = useUpdateWorkspaceAuditLogsRetention(); | ||
|
||
const { currentWorkspace } = useWorkspace(); | ||
const { membership } = useProjectPermission(); | ||
const { subscription } = useSubscription(); | ||
const { popUp, handlePopUpOpen, handlePopUpToggle } = usePopUp(["upgradePlan"] as const); | ||
|
||
const { | ||
control, | ||
formState: { isSubmitting, isDirty }, | ||
handleSubmit | ||
} = useForm<TForm>({ | ||
resolver: zodResolver(formSchema), | ||
values: { | ||
auditLogsRetentionDays: | ||
currentWorkspace?.auditLogsRetentionDays ?? subscription?.auditLogsRetentionDays ?? 0 | ||
} | ||
}); | ||
|
||
if (!currentWorkspace) return null; | ||
|
||
const handleAuditLogsRetentionSubmit = async ({ auditLogsRetentionDays }: TForm) => { | ||
try { | ||
if (!subscription?.auditLogs) { | ||
handlePopUpOpen("upgradePlan", { | ||
description: "You can only configure audit logs retention if you upgrade your plan." | ||
}); | ||
|
||
return; | ||
} | ||
|
||
if (subscription && auditLogsRetentionDays > subscription?.auditLogsRetentionDays) { | ||
handlePopUpOpen("upgradePlan", { | ||
description: | ||
"To update your audit logs retention period to a higher value, upgrade your plan." | ||
}); | ||
|
||
return; | ||
} | ||
|
||
await updateAuditLogsRetention({ | ||
auditLogsRetentionDays, | ||
projectSlug: currentWorkspace.slug | ||
}); | ||
|
||
createNotification({ | ||
text: "Successfully updated audit logs retention period", | ||
type: "success" | ||
}); | ||
} catch (err) { | ||
createNotification({ | ||
text: "Failed updating audit logs retention period", | ||
type: "error" | ||
}); | ||
} | ||
}; | ||
|
||
const isAdmin = membership.roles.includes(ProjectMembershipRole.Admin); | ||
return ( | ||
<> | ||
<div className="mb-6 rounded-lg border border-mineshaft-600 bg-mineshaft-900 p-4"> | ||
<div className="flex w-full items-center justify-between"> | ||
<p className="text-xl font-semibold">Audit Logs Retention</p> | ||
</div> | ||
<p className="mb-4 mt-2 max-w-2xl text-sm text-gray-400"> | ||
Set the number of days to keep your project audit logs. | ||
</p> | ||
<form onSubmit={handleSubmit(handleAuditLogsRetentionSubmit)} autoComplete="off"> | ||
<div className="max-w-xs"> | ||
<Controller | ||
control={control} | ||
defaultValue={0} | ||
name="auditLogsRetentionDays" | ||
render={({ field, fieldState: { error } }) => ( | ||
<FormControl | ||
isError={Boolean(error)} | ||
errorText={error?.message} | ||
label="Number of days" | ||
> | ||
<Input {...field} type="number" min={1} step={1} isDisabled={!isAdmin} /> | ||
</FormControl> | ||
)} | ||
/> | ||
</div> | ||
<Button | ||
colorSchema="secondary" | ||
type="submit" | ||
isLoading={isSubmitting} | ||
disabled={!isAdmin || !isDirty} | ||
> | ||
Save | ||
</Button> | ||
</form> | ||
</div> | ||
<UpgradePlanModal | ||
isOpen={popUp.upgradePlan.isOpen} | ||
onOpenChange={(isOpen) => handlePopUpToggle("upgradePlan", isOpen)} | ||
text={(popUp.upgradePlan?.data as { description: string })?.description} | ||
/> | ||
</> | ||
); | ||
}; |
1 change: 1 addition & 0 deletions
1
...end/src/views/Settings/ProjectSettingsPage/components/AuditLogsRetentionSection/index.tsx
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 @@ | ||
export { AuditLogsRetentionSection } from "./AuditLogsRetentionSection"; |
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