2025-06-30 18:58:24 +02:00
|
|
|
/*
|
|
|
|
SPDX-FileCopyrightText: © 2025 Hornwitser <code@hornwitser.no>
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
*/
|
2025-06-23 00:00:18 +02:00
|
|
|
import type { ApiUser, ApiUserType } from "~/shared/types/api";
|
2025-06-24 15:19:11 +02:00
|
|
|
import type { Id } from "~/shared/types/common";
|
2025-06-23 00:00:18 +02:00
|
|
|
import { DateTime, Zone } from "~/shared/utils/luxon";
|
2025-06-23 22:46:39 +02:00
|
|
|
import { ClientEntity } from "~/utils/client-entity";
|
2025-06-23 00:00:18 +02:00
|
|
|
|
2025-06-24 15:19:11 +02:00
|
|
|
export class ClientUser extends ClientEntity<ApiUser> {
|
2025-06-23 00:00:18 +02:00
|
|
|
serverName: string | undefined;
|
|
|
|
serverType: ApiUserType
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
id: Id,
|
|
|
|
updatedAt: DateTime,
|
|
|
|
deleted: boolean,
|
|
|
|
public name: string | undefined,
|
|
|
|
public type: ApiUserType,
|
|
|
|
) {
|
|
|
|
super(id, updatedAt, deleted);
|
|
|
|
this.serverName = name;
|
|
|
|
this.serverType = type;
|
|
|
|
}
|
|
|
|
|
|
|
|
override isModified() {
|
|
|
|
return (
|
|
|
|
super.isModified()
|
|
|
|
|| this.name !== this.serverName
|
|
|
|
|| this.type !== this.serverType
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
override discard() {
|
|
|
|
if (this.isNew()) {
|
|
|
|
throw new Error("ClientUser.discard: Cannot discard new entity.")
|
|
|
|
}
|
|
|
|
this.updatedAt = this.serverUpdatedAt;
|
2025-06-23 22:46:39 +02:00
|
|
|
this.deleted = this.serverDeleted;
|
2025-06-23 00:00:18 +02:00
|
|
|
this.name = this.serverName;
|
|
|
|
this.type = this.serverType;
|
|
|
|
}
|
|
|
|
|
|
|
|
static create(
|
|
|
|
id: Id,
|
|
|
|
name: string | undefined,
|
|
|
|
type: ApiUserType,
|
|
|
|
opts: { zone: Zone, locale: string },
|
|
|
|
) {
|
|
|
|
return new this(
|
|
|
|
id,
|
2025-06-23 22:46:39 +02:00
|
|
|
DateTime.fromMillis(ClientEntity.newEntityMillis, opts),
|
2025-06-23 00:00:18 +02:00
|
|
|
false,
|
|
|
|
name,
|
|
|
|
type,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2025-06-24 15:19:11 +02:00
|
|
|
static fromApi(api: ApiUser, opts: { zone: Zone, locale: string }) {
|
2025-06-23 00:00:18 +02:00
|
|
|
return new this(
|
|
|
|
api.id,
|
|
|
|
DateTime.fromISO(api.updatedAt, opts),
|
|
|
|
false,
|
|
|
|
api.name,
|
|
|
|
api.type,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2025-06-24 15:19:11 +02:00
|
|
|
override apiUpdate(api: ApiUser, opts: { zone: Zone, locale: string }) {
|
2025-06-23 00:00:18 +02:00
|
|
|
const wasModified = this.isModified();
|
|
|
|
this.serverUpdatedAt = DateTime.fromISO(api.updatedAt, opts);
|
|
|
|
this.serverDeleted = false;
|
|
|
|
this.serverName = api.name;
|
|
|
|
this.serverType = api.type;
|
|
|
|
if (!wasModified || !this.isModified()) {
|
|
|
|
this.discard();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
toApi(): ApiUser {
|
|
|
|
return {
|
|
|
|
id: this.id,
|
|
|
|
updatedAt: toIso(this.updatedAt),
|
|
|
|
name: this.name,
|
|
|
|
type: this.type,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|