51 lines
1.1 KiB
TypeScript
51 lines
1.1 KiB
TypeScript
import fs from "fs";
|
|
import grabDirNames from "../names/grab-dir-names";
|
|
import grabConfig from "./grab-config";
|
|
import _ from "lodash";
|
|
import { SiteConfig } from "../../../types";
|
|
|
|
type Params = {
|
|
userId?: string | number;
|
|
newConfig?: SiteConfig;
|
|
};
|
|
|
|
type Return = {
|
|
success?: boolean;
|
|
msg?: string;
|
|
};
|
|
|
|
export default function updateUserConfig({
|
|
newConfig,
|
|
userId,
|
|
}: Params): Return {
|
|
if (!userId || !newConfig) {
|
|
return {
|
|
success: false,
|
|
msg: `UserID or newConfig not provided`,
|
|
};
|
|
}
|
|
|
|
const { userConfigJSONFilePath } = grabDirNames({
|
|
userId,
|
|
});
|
|
|
|
if (!userConfigJSONFilePath || !fs.existsSync(userConfigJSONFilePath)) {
|
|
return {
|
|
success: false,
|
|
msg: `userConfigJSONFilePath not found!`,
|
|
};
|
|
}
|
|
|
|
const { userConfig: existingUserConfig } = grabConfig({ userId });
|
|
|
|
const updateConfig = _.merge(existingUserConfig, newConfig);
|
|
|
|
fs.writeFileSync(
|
|
userConfigJSONFilePath,
|
|
JSON.stringify(updateConfig),
|
|
"utf-8"
|
|
);
|
|
|
|
return { success: true };
|
|
}
|