2023-03-09 08:14:29 +00:00
|
|
|
import { SettingsManager } from "@saleor/app-sdk/settings-manager";
|
2023-05-31 16:08:43 +00:00
|
|
|
import { SmtpConfig } from "./smtp-config-schema";
|
2023-03-09 08:14:29 +00:00
|
|
|
|
2023-05-31 16:08:43 +00:00
|
|
|
export class SmtpPrivateMetadataManager {
|
|
|
|
private metadataKey = "smtp-config";
|
2023-03-09 08:14:29 +00:00
|
|
|
|
|
|
|
constructor(private metadataManager: SettingsManager, private saleorApiUrl: string) {}
|
|
|
|
|
2023-05-31 16:08:43 +00:00
|
|
|
getConfig(): Promise<SmtpConfig | undefined> {
|
2023-03-09 08:14:29 +00:00
|
|
|
return this.metadataManager.get(this.metadataKey, this.saleorApiUrl).then((data) => {
|
|
|
|
if (!data) {
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
return JSON.parse(data);
|
|
|
|
} catch (e) {
|
|
|
|
throw new Error("Invalid metadata value, can't be parsed");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-05-31 16:08:43 +00:00
|
|
|
setConfig(config: SmtpConfig): Promise<void> {
|
2023-03-09 08:14:29 +00:00
|
|
|
return this.metadataManager.set({
|
|
|
|
key: this.metadataKey,
|
|
|
|
value: JSON.stringify(config),
|
|
|
|
domain: this.saleorApiUrl,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|