mirror of
https://github.com/outline/outline.git
synced 2026-11-24 23:00:18 +03:00
chore: Share import state lists from the Import model (#14575)
This commit is contained in:
1 parent
4fd8b0731e
commit
6fd7eb7cf0
4 files changed
+25
-35
No files matched your search
+15
-5
@@ -44,6 +44,20 @@ class Import<T extends ImportableIntegrationService> extends ParanoidModel<
|
||||
InferAttributes<Import<T>>,
|
||||
Partial<InferCreationAttributes<Import<T>>>
|
||||
> {
|
||||
/** States in which an import is still running and can be canceled. */
|
||||
public static readonly activeStates: readonly ImportState[] = [
|
||||
ImportState.Created,
|
||||
ImportState.InProgress,
|
||||
ImportState.Processed,
|
||||
];
|
||||
|
||||
/** States in which an import has stopped and will not change again. */
|
||||
public static readonly terminalStates: readonly ImportState[] = [
|
||||
ImportState.Completed,
|
||||
ImportState.Errored,
|
||||
ImportState.Canceled,
|
||||
];
|
||||
|
||||
@NotContainsUrl
|
||||
@Length({
|
||||
max: ImportValidation.maxNameLength,
|
||||
@@ -137,11 +151,7 @@ class Import<T extends ImportableIntegrationService> extends ParanoidModel<
|
||||
const inProgress = await this.count({
|
||||
where: {
|
||||
teamId: model.teamId,
|
||||
state: [
|
||||
ImportState.Created,
|
||||
ImportState.InProgress,
|
||||
ImportState.Processed,
|
||||
],
|
||||
state: [...Import.activeStates],
|
||||
},
|
||||
transaction,
|
||||
});
|
||||
|
||||
@@ -1,13 +1,6 @@
|
||||
import { ImportState } from "@shared/types";
|
||||
import { User, Team, Import } from "@server/models";
|
||||
import { allow, can } from "./cancan";
|
||||
import { and, isTeamAdmin, isTeamMutable, or } from "./utils";
|
||||
|
||||
const TerminalStates = [
|
||||
ImportState.Completed,
|
||||
ImportState.Errored,
|
||||
ImportState.Canceled,
|
||||
];
|
||||
import { and, isTeamAdmin, isTeamMutable } from "./utils";
|
||||
|
||||
allow(User, ["createImport", "listImports"], Team, (actor, team) =>
|
||||
and(isTeamAdmin(actor, team), isTeamMutable(actor))
|
||||
@@ -20,7 +13,7 @@ allow(User, "read", Import, (actor, importModel) =>
|
||||
allow(User, "delete", Import, (actor, importModel) =>
|
||||
and(
|
||||
can(actor, "read", importModel),
|
||||
!!importModel && TerminalStates.includes(importModel.state)
|
||||
!!importModel && Import.terminalStates.includes(importModel.state)
|
||||
)
|
||||
);
|
||||
|
||||
@@ -28,10 +21,6 @@ allow(User, "cancel", Import, (actor, importModel) =>
|
||||
and(
|
||||
can(actor, "read", importModel),
|
||||
importModel?.createdById === actor.id,
|
||||
or(
|
||||
importModel?.state === ImportState.Created,
|
||||
importModel?.state === ImportState.InProgress,
|
||||
importModel?.state === ImportState.Processed
|
||||
)
|
||||
!!importModel && Import.activeStates.includes(importModel.state)
|
||||
)
|
||||
);
|
||||
@@ -40,13 +40,6 @@ export type ProcessOutput<T extends ImportableIntegrationService> = {
|
||||
childTasksInput: ImportTaskInput<T>;
|
||||
};
|
||||
|
||||
const FinishedImportStates = [
|
||||
ImportState.Processed,
|
||||
ImportState.Completed,
|
||||
ImportState.Errored,
|
||||
ImportState.Canceled,
|
||||
];
|
||||
|
||||
type Props = {
|
||||
/** id of the import_task */
|
||||
importTaskId: string;
|
||||
@@ -78,9 +71,12 @@ export default abstract class APIImportTask<
|
||||
return;
|
||||
}
|
||||
|
||||
// Don't process any further when the associated import is canceled by the
|
||||
// user or has already finished.
|
||||
if (FinishedImportStates.includes(importTask.import.state)) {
|
||||
// Stop when the import has ended, or its tasks are all processed already.
|
||||
const { state } = importTask.import;
|
||||
if (
|
||||
state === ImportState.Processed ||
|
||||
Import.terminalStates.includes(state)
|
||||
) {
|
||||
if (importTask.state !== ImportTaskState.Completed) {
|
||||
importTask.state = ImportTaskState.Canceled;
|
||||
await importTask.save();
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import { subDays } from "date-fns";
|
||||
import { Op } from "sequelize";
|
||||
import { ImportState } from "@shared/types";
|
||||
import Logger from "@server/logging/Logger";
|
||||
import { Import, ImportTask } from "@server/models";
|
||||
import { TaskPriority } from "./base/BaseTask";
|
||||
@@ -22,11 +21,7 @@ export default class CleanupOldImportsTask extends CronTask {
|
||||
{
|
||||
attributes: ["id"],
|
||||
where: {
|
||||
state: [
|
||||
ImportState.Completed,
|
||||
ImportState.Errored,
|
||||
ImportState.Canceled,
|
||||
],
|
||||
state: [...Import.terminalStates],
|
||||
createdAt: {
|
||||
[Op.lt]: cutoffDate,
|
||||
},
|
||||
|
||||
Reference in new issue
Block a user