
* feat: ✨ add ping method to avatax-client * refactor: ♻️ use avatax-auth-validation.service instead of address service * refactor: ♻️ extract avatax-configuration-address-fragment * refactor: ♻️ extract avatax-configuration-credentials-fragment * refactor: ♻️ extract form-helper-text * refactor: ♻️ extract form-section * refactor: ♻️ extract avatax-configuration-taxes-fragment * feat: ✨ move verify to credentials fragment && add disabled form section * refactor: 🚚 obfuscator * feat: ✨ add separate credentials and address validation services * build: 👷 add changeset * feat: ✨ add address resolution message * fix: 🐛 changeset * refactor: ♻️ extract avataxAddressResolutionProcessor and add tests * refactor: * refactor: ♻️ remove brs from avatax-instructions * refactor: ♻️ replace b with Text bodyStrong * refactor: ♻️ state tuple to object * refactor: ♻️ destructure some more constructors * refactor: ♻️ memoize isLoadings & handlers
81 lines
2.4 KiB
TypeScript
81 lines
2.4 KiB
TypeScript
import { DeepPartial } from "@trpc/server";
|
|
import { Client } from "urql";
|
|
import { Logger, createLogger } from "../../../lib/logger";
|
|
import { createSettingsManager } from "../../app/metadata-manager";
|
|
import { AvataxConfig, AvataxConnection } from "../avatax-connection-schema";
|
|
import { AvataxConnectionRepository } from "./avatax-connection-repository";
|
|
import { AvataxAuthValidationService } from "./avatax-auth-validation.service";
|
|
import { AvataxClient } from "../avatax-client";
|
|
|
|
export class AvataxConnectionService {
|
|
private logger: Logger;
|
|
private avataxConnectionRepository: AvataxConnectionRepository;
|
|
|
|
constructor({
|
|
client,
|
|
appId,
|
|
saleorApiUrl,
|
|
}: {
|
|
client: Client;
|
|
appId: string;
|
|
saleorApiUrl: string;
|
|
}) {
|
|
this.logger = createLogger({
|
|
name: "AvataxConnectionService",
|
|
});
|
|
|
|
const settingsManager = createSettingsManager(client, appId);
|
|
|
|
this.avataxConnectionRepository = new AvataxConnectionRepository(settingsManager, saleorApiUrl);
|
|
}
|
|
|
|
private async checkIfAuthorized(input: AvataxConfig) {
|
|
const avataxClient = new AvataxClient(input);
|
|
const authValidationService = new AvataxAuthValidationService(avataxClient);
|
|
|
|
await authValidationService.validate();
|
|
}
|
|
|
|
getAll(): Promise<AvataxConnection[]> {
|
|
return this.avataxConnectionRepository.getAll();
|
|
}
|
|
|
|
getById(id: string): Promise<AvataxConnection> {
|
|
return this.avataxConnectionRepository.get(id);
|
|
}
|
|
|
|
async create(input: AvataxConfig): Promise<{ id: string }> {
|
|
await this.checkIfAuthorized(input);
|
|
|
|
return this.avataxConnectionRepository.post(input);
|
|
}
|
|
|
|
async update(id: string, nextConfigPartial: DeepPartial<AvataxConfig>): Promise<void> {
|
|
const data = await this.getById(id);
|
|
// omit the key "id" from the result
|
|
const { id: _, ...setting } = data;
|
|
const prevConfig = setting.config;
|
|
|
|
// todo: add deepRightMerge
|
|
const input: AvataxConfig = {
|
|
...prevConfig,
|
|
...nextConfigPartial,
|
|
credentials: {
|
|
...prevConfig.credentials,
|
|
...nextConfigPartial.credentials,
|
|
},
|
|
address: {
|
|
...prevConfig.address,
|
|
...nextConfigPartial.address,
|
|
},
|
|
};
|
|
|
|
await this.checkIfAuthorized(input);
|
|
|
|
return this.avataxConnectionRepository.patch(id, { config: input });
|
|
}
|
|
|
|
async delete(id: string): Promise<void> {
|
|
return this.avataxConnectionRepository.delete(id);
|
|
}
|
|
}
|