refactor: service address
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
/**
|
||||
* Address implementation class for Mail Manager services
|
||||
*
|
||||
* Plain value object (no mutation tracking). ServiceObject getters return
|
||||
* fresh instances built from raw data; mutating a getter result does not
|
||||
* write back — assign through the setter to persist changes.
|
||||
*/
|
||||
|
||||
import type { ServiceAddressInterface } from '@/types/service';
|
||||
|
||||
export class ServiceAddressObject implements ServiceAddressInterface {
|
||||
|
||||
_address: string;
|
||||
_label: string | null;
|
||||
|
||||
constructor(address: string = '', label: string | null = null) {
|
||||
this._address = address;
|
||||
this._label = label;
|
||||
}
|
||||
|
||||
static fromJson(data: ServiceAddressInterface): ServiceAddressObject {
|
||||
return new ServiceAddressObject(data.address, data.label ?? null);
|
||||
}
|
||||
|
||||
toJson(): ServiceAddressInterface {
|
||||
const label = this._label?.trim() ?? '';
|
||||
return {
|
||||
address: this._address.trim(),
|
||||
label: label.length > 0 ? label : null,
|
||||
};
|
||||
}
|
||||
|
||||
toJSON(): ServiceAddressInterface {
|
||||
return this.toJson();
|
||||
}
|
||||
|
||||
clone(): ServiceAddressObject {
|
||||
return new ServiceAddressObject(this._address, this._label);
|
||||
}
|
||||
|
||||
/** Case-insensitive address comparison, ignoring labels */
|
||||
matches(address: string): boolean {
|
||||
return this._address.trim().toLowerCase() === address.trim().toLowerCase();
|
||||
}
|
||||
|
||||
equals(other: ServiceAddressObject | null | undefined): boolean {
|
||||
if (!other) {
|
||||
return false;
|
||||
}
|
||||
const current = this.toJson();
|
||||
const next = other.toJson();
|
||||
return current.address === next.address && (current.label ?? null) === (next.label ?? null);
|
||||
}
|
||||
|
||||
/** Display form: "Label <address>" or bare address */
|
||||
format(): string {
|
||||
const { address, label } = this.toJson();
|
||||
return label ? `${label} <${address}>` : address;
|
||||
}
|
||||
|
||||
get empty(): boolean {
|
||||
return this._address.trim().length === 0;
|
||||
}
|
||||
|
||||
/** Properties (raw values for editing; normalization happens in toJson) */
|
||||
|
||||
get address(): string {
|
||||
return this._address;
|
||||
}
|
||||
|
||||
set address(value: string) {
|
||||
this._address = value;
|
||||
}
|
||||
|
||||
get label(): string | null {
|
||||
return this._label;
|
||||
}
|
||||
|
||||
set label(value: string | null) {
|
||||
this._label = value;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
export { ProviderObject } from './provider';
|
||||
export { ServiceObject } from './service';
|
||||
export { ServiceAddressObject } from './address';
|
||||
export {
|
||||
CollectionObject,
|
||||
CollectionPropertiesObject
|
||||
@@ -7,6 +8,7 @@ export {
|
||||
export { EntityObject } from './entity';
|
||||
export {
|
||||
MessageObject,
|
||||
MessageAddressObject,
|
||||
MessagePartObject
|
||||
} from './message';
|
||||
export {
|
||||
|
||||
@@ -8,6 +8,7 @@ import type {
|
||||
ServiceLocation,
|
||||
ServiceModelInterface
|
||||
} from "@/types/service";
|
||||
import { ServiceAddressObject } from './address';
|
||||
import { Identity } from './identity';
|
||||
import { Location } from './location';
|
||||
import { MutationProxy } from './mutation-proxy';
|
||||
@@ -118,20 +119,20 @@ export class ServiceObject implements ServiceModelInterface {
|
||||
return this._data.capabilities ?? {};
|
||||
}
|
||||
|
||||
get primaryAddress(): string | null {
|
||||
return this._data.primaryAddress ?? null;
|
||||
get primaryAddress(): ServiceAddressObject | null {
|
||||
return this._data.primaryAddress ? ServiceAddressObject.fromJson(this._data.primaryAddress) : null;
|
||||
}
|
||||
|
||||
set primaryAddress(value: string | null) {
|
||||
this._data.primaryAddress = value;
|
||||
set primaryAddress(value: ServiceAddressObject | null) {
|
||||
this._data.primaryAddress = value ? value.toJson() : null;
|
||||
}
|
||||
|
||||
get secondaryAddresses(): string[] {
|
||||
return this._data.secondaryAddresses ?? [];
|
||||
get secondaryAddresses(): ServiceAddressObject[] {
|
||||
return (this._data.secondaryAddresses ?? []).map(entry => ServiceAddressObject.fromJson(entry));
|
||||
}
|
||||
|
||||
set secondaryAddresses(value: string[] | null) {
|
||||
this._data.secondaryAddresses = value;
|
||||
set secondaryAddresses(value: ServiceAddressObject[] | null) {
|
||||
this._data.secondaryAddresses = value ? value.map(entry => entry.toJson()) : null;
|
||||
}
|
||||
|
||||
/** Mutable Properties */
|
||||
|
||||
Reference in New Issue
Block a user