166 lines
4.3 KiB
TypeScript
166 lines
4.3 KiB
TypeScript
import {
|
|
INITIAL_CLIENTS,
|
|
INITIAL_DEALS,
|
|
INITIAL_CATALOG,
|
|
INITIAL_INVENTORY,
|
|
INITIAL_RECIPES,
|
|
INITIAL_EMPLOYEES,
|
|
INITIAL_TICKETS,
|
|
INITIAL_CONVERSATIONS,
|
|
INITIAL_FINANCE,
|
|
INITIAL_TASKS,
|
|
ClientEntity,
|
|
DealEntity,
|
|
CatalogItemEntity,
|
|
InventoryItemEntity,
|
|
RecipeItem,
|
|
EmployeeEntity,
|
|
TicketEntity,
|
|
ConversationItem,
|
|
FinanceEntry,
|
|
TaskEntity,
|
|
} from './mockData';
|
|
|
|
const delay = (ms = 250) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
|
|
class MockApiService {
|
|
private clients: ClientEntity[] = [...INITIAL_CLIENTS];
|
|
private deals: DealEntity[] = [...INITIAL_DEALS];
|
|
private catalog: CatalogItemEntity[] = [...INITIAL_CATALOG];
|
|
private inventory: InventoryItemEntity[] = [...INITIAL_INVENTORY];
|
|
private recipes: RecipeItem[] = [...INITIAL_RECIPES];
|
|
private employees: EmployeeEntity[] = [...INITIAL_EMPLOYEES];
|
|
private tickets: TicketEntity[] = [...INITIAL_TICKETS];
|
|
private conversations: ConversationItem[] = [...INITIAL_CONVERSATIONS];
|
|
private finance: FinanceEntry[] = [...INITIAL_FINANCE];
|
|
private tasks: TaskEntity[] = [...INITIAL_TASKS];
|
|
|
|
// Customers / Clients
|
|
async getCustomers(): Promise<ClientEntity[]> {
|
|
await delay(200);
|
|
return [...this.clients];
|
|
}
|
|
|
|
async createCustomer(data: Omit<ClientEntity, 'id'>): Promise<ClientEntity> {
|
|
await delay(300);
|
|
const newClient: ClientEntity = {
|
|
...data,
|
|
id: `cl-${Date.now()}`,
|
|
};
|
|
this.clients = [newClient, ...this.clients];
|
|
return newClient;
|
|
}
|
|
|
|
// Deals
|
|
async getDeals(): Promise<DealEntity[]> {
|
|
await delay(200);
|
|
return [...this.deals];
|
|
}
|
|
|
|
async updateDealStage(id: string, stage: DealEntity['stage']): Promise<void> {
|
|
await delay(150);
|
|
this.deals = this.deals.map((d) => (d.id === id ? { ...d, stage } : d));
|
|
}
|
|
|
|
// Catalog
|
|
async getCatalog(): Promise<CatalogItemEntity[]> {
|
|
await delay(200);
|
|
return [...this.catalog];
|
|
}
|
|
|
|
async toggleStopList(id: string): Promise<boolean> {
|
|
await delay(150);
|
|
let updated = false;
|
|
this.catalog = this.catalog.map((item) => {
|
|
if (item.id === id) {
|
|
updated = !item.isStopListed;
|
|
return { ...item, isStopListed: updated };
|
|
}
|
|
return item;
|
|
});
|
|
return updated;
|
|
}
|
|
|
|
// Inventory
|
|
async getInventory(): Promise<InventoryItemEntity[]> {
|
|
await delay(200);
|
|
return [...this.inventory];
|
|
}
|
|
|
|
// Recipes
|
|
async getRecipes(): Promise<RecipeItem[]> {
|
|
await delay(200);
|
|
return [...this.recipes];
|
|
}
|
|
|
|
// Employees
|
|
async getEmployees(): Promise<EmployeeEntity[]> {
|
|
await delay(200);
|
|
return [...this.employees];
|
|
}
|
|
|
|
// HelpDesk Tickets
|
|
async getTickets(): Promise<TicketEntity[]> {
|
|
await delay(200);
|
|
return [...this.tickets];
|
|
}
|
|
|
|
// Communications / Conversations
|
|
async getMessages(): Promise<ConversationItem[]> {
|
|
await delay(200);
|
|
return [...this.conversations];
|
|
}
|
|
|
|
async sendMessage(convId: string, text: string): Promise<void> {
|
|
await delay(150);
|
|
this.conversations = this.conversations.map((c) => {
|
|
if (c.id === convId) {
|
|
return {
|
|
...c,
|
|
lastMessage: text,
|
|
time: new Date().toLocaleTimeString('ru-RU', { hour: '2-digit', minute: '2-digit' }),
|
|
messages: [
|
|
...c.messages,
|
|
{
|
|
id: `msg-${Date.now()}`,
|
|
sender: 'agent',
|
|
senderName: 'Вы (Оператор)',
|
|
text,
|
|
time: new Date().toLocaleTimeString('ru-RU', { hour: '2-digit', minute: '2-digit' }),
|
|
},
|
|
],
|
|
};
|
|
}
|
|
return c;
|
|
});
|
|
}
|
|
|
|
// Finance
|
|
async getFinance(): Promise<FinanceEntry[]> {
|
|
await delay(200);
|
|
return [...this.finance];
|
|
}
|
|
|
|
async addFinanceEntry(entry: Omit<FinanceEntry, 'id'>): Promise<FinanceEntry> {
|
|
await delay(250);
|
|
const newEntry: FinanceEntry = {
|
|
...entry,
|
|
id: `fin-${Date.now()}`,
|
|
};
|
|
this.finance = [newEntry, ...this.finance];
|
|
return newEntry;
|
|
}
|
|
|
|
// Tasks
|
|
async getTasks(): Promise<TaskEntity[]> {
|
|
await delay(200);
|
|
return [...this.tasks];
|
|
}
|
|
|
|
async updateTaskStatus(taskId: string, status: TaskEntity['status']): Promise<void> {
|
|
await delay(150);
|
|
this.tasks = this.tasks.map((t) => (t.id === taskId ? { ...t, status } : t));
|
|
}
|
|
}
|
|
|
|
export const mockApi = new MockApiService();
|