feat: role system

This commit is contained in:
2026-03-18 20:11:07 +03:00
parent 73070fe233
commit c3127b8d47
47 changed files with 2425 additions and 768 deletions

View File

@@ -1,30 +1,9 @@
import { authInstance, languageInstance } from "@shared";
import { authInstance, languageInstance, mobxFetch } from "@shared";
import { makeAutoObservable, runInAction } from "mobx";
import { getVehicleSessionsApi } from "./api";
import { Vehicle, VehicleMaintenanceSession } from "./types";
export type Vehicle = {
vehicle: {
id: number;
tail_number: string;
type: number;
carrier_id: number;
carrier: string;
uuid?: string;
model?: string;
current_snapshot_uuid?: string;
snapshot_update_blocked?: boolean;
demo_mode_enabled?: boolean;
maintenance_mode_on?: boolean;
city_id?: number;
};
device_status?: {
device_uuid: string;
online: boolean;
gps_ok: boolean;
media_service_ok: boolean;
last_update: string;
is_connected: boolean;
};
};
export type { Vehicle, VehicleMaintenanceSession } from "./types";
class VehicleStore {
vehicles: {
@@ -35,6 +14,9 @@ class VehicleStore {
loaded: false,
};
vehicle: Record<string, Vehicle> = {};
vehicleSessions: VehicleMaintenanceSession[] | null = null;
vehicleSessionsLoading = false;
vehicleSessionsError: string | null = null;
constructor() {
makeAutoObservable(this);
@@ -89,7 +71,7 @@ class VehicleStore {
if (updatedUuid != null) {
const entry = Object.entries(this.vehicle).find(
([, item]) => item.vehicle.uuid === updatedUuid
([, item]) => item.vehicle.uuid === updatedUuid,
);
if (entry) {
@@ -118,7 +100,7 @@ class VehicleStore {
runInAction(() => {
this.vehicles.data = this.vehicles.data.filter(
(vehicle) => vehicle.vehicle.id !== id
(vehicle) => vehicle.vehicle.id !== id,
);
});
};
@@ -137,7 +119,7 @@ class VehicleStore {
type: number,
carrier: string,
carrierId: number,
model?: string
model?: string,
) => {
const payload: Record<string, unknown> = {
tail_number: tailNumber,
@@ -197,7 +179,7 @@ class VehicleStore {
carrier_id: number;
model?: string;
snapshot_update_blocked?: boolean;
}
},
) => {
const payload: Record<string, unknown> = {
tail_number: data.tail_number,
@@ -210,7 +192,7 @@ class VehicleStore {
payload.snapshot_update_blocked = data.snapshot_update_blocked;
const response = await languageInstance("ru").patch(
`/vehicle/${id}`,
payload
payload,
);
const normalizedVehicle = this.normalizeVehicleItem(response.data);
const updatedVehiclePayload = {
@@ -230,9 +212,12 @@ class VehicleStore {
};
setMaintenanceMode = async (uuid: string, enabled: boolean) => {
const response = await authInstance.post(`/devices/${uuid}/maintenance-mode`, {
enabled,
});
const response = await authInstance.post(
`/devices/${uuid}/maintenance-mode`,
{
enabled,
},
);
const normalizedVehicle = this.normalizeVehicleItem(response.data);
runInAction(() => {
@@ -255,10 +240,24 @@ class VehicleStore {
this.mergeVehicleInCaches({
...normalizedVehicle.vehicle,
uuid,
demo_mode_enabled: normalizedVehicle.vehicle.demo_mode_enabled ?? enabled,
demo_mode_enabled:
normalizedVehicle.vehicle.demo_mode_enabled ?? enabled,
});
});
};
getVehicleSessions = mobxFetch<
number,
VehicleMaintenanceSession[],
VehicleStore
>({
store: this,
value: "vehicleSessions",
loading: "vehicleSessionsLoading",
error: "vehicleSessionsError",
resetValue: true,
fn: getVehicleSessionsApi,
});
}
export const vehicleStore = new VehicleStore();