79 lines
2.2 KiB
TypeScript
79 lines
2.2 KiB
TypeScript
import _ from "lodash";
|
|
import path from "path";
|
|
import writeBacupFiles from "./write-backup-files";
|
|
import { APIResponseObject } from "../../../../../types";
|
|
import grabDirNames from "../../../../../utils/backend/names/grab-dir-names";
|
|
import {
|
|
DSQL_DATASQUIREL_BACKUPS,
|
|
DsqlTables,
|
|
} from "../../../../../types/dsql";
|
|
import addDbEntry from "../../../db/addDbEntry";
|
|
import numberfy from "../../../../../utils/numberfy";
|
|
import dbGrabUserResource from "../../../../web-app/db/grab-user-resource";
|
|
|
|
type Params = {
|
|
targetUserId?: string | number;
|
|
};
|
|
|
|
export default async function suAddBackup({
|
|
targetUserId,
|
|
}: Params): Promise<APIResponseObject> {
|
|
try {
|
|
const { mainBackupDir, userBackupDir } = grabDirNames({
|
|
userId: targetUserId,
|
|
});
|
|
|
|
if (targetUserId && !userBackupDir) {
|
|
return {
|
|
success: false,
|
|
msg: `Error grabbing user backup directory`,
|
|
};
|
|
}
|
|
|
|
const newBackup: DSQL_DATASQUIREL_BACKUPS = {
|
|
user_id: targetUserId ? numberfy(targetUserId) : undefined,
|
|
};
|
|
|
|
const newBackupEntry = await addDbEntry<
|
|
DSQL_DATASQUIREL_BACKUPS,
|
|
(typeof DsqlTables)[number]
|
|
>({
|
|
tableName: "backups",
|
|
data: newBackup,
|
|
});
|
|
|
|
if (!newBackupEntry?.payload?.insertId) {
|
|
return {
|
|
success: false,
|
|
msg: `Couldn't create new backup entry`,
|
|
};
|
|
}
|
|
|
|
const { single: newlyAddedBackup } =
|
|
await dbGrabUserResource<DSQL_DATASQUIREL_BACKUPS>({
|
|
tableName: "backups",
|
|
targetID: newBackupEntry.payload?.insertId,
|
|
isSuperUser: true,
|
|
});
|
|
|
|
if (!newlyAddedBackup?.id) {
|
|
return {
|
|
success: false,
|
|
msg: `Couldn't fetch newly added backup`,
|
|
};
|
|
}
|
|
|
|
const writeBackup = await writeBacupFiles({
|
|
backup: newlyAddedBackup,
|
|
});
|
|
|
|
return writeBackup;
|
|
} catch (error: any) {
|
|
return {
|
|
success: false,
|
|
msg: `Backup add failed`,
|
|
error: error.message,
|
|
};
|
|
}
|
|
}
|