Files
outline/server/queues/processors/ApiKeyCleanupProcessor.ts
Tom Moor bf45e97641 chore: Enforce type import consistency (#10968)
* Update types

* fix circular dep

* type imports

* lint type imports and --fix
2025-12-19 23:07:02 -05:00

44 lines
1.2 KiB
TypeScript

import { Op } from "sequelize";
import { TeamPreference, UserRole } from "@shared/types";
import Logger from "@server/logging/Logger";
import { ApiKey, User } from "@server/models";
import type { Event, TeamEvent } from "@server/types";
import BaseProcessor from "./BaseProcessor";
export default class ApiKeyCleanupProcessor extends BaseProcessor {
static applicableEvents: Event["name"][] = ["teams.update"];
async perform(event: TeamEvent) {
// If the team preference to allow members to create API keys has been disabled, we need to
// clean up any existing API keys created by non-admins as they are no longer valid.
if (
event.changes?.attributes.preferences?.[
TeamPreference.MembersCanCreateApiKey
] === false
) {
const userIds = (
await User.findAll({
attributes: ["id"],
where: {
teamId: event.teamId,
role: {
[Op.ne]: UserRole.Admin,
},
},
})
).map((u) => u.id);
const count = await ApiKey.destroy({
where: {
userId: userIds,
},
});
Logger.info(
"processor",
`Deleted ${count} API keys for non-admin users in team ${event.teamId}`
);
}
}
}