Add category management screen
This commit is contained in:
1 parent
f359b44416
commit
6d363d1c43
8 files changed
+267
-10
No files matched your search
@@ -1,5 +1,7 @@
|
||||
import Dexie, { Table } from 'dexie';
|
||||
import { v4 as uuidv4 } from 'uuid';
|
||||
import { Area } from '../models/Area';
|
||||
import { Category } from '../models/Category';
|
||||
import { PickItem } from '../models/PickItem';
|
||||
import { PickList } from '../models/PickList';
|
||||
import { Product } from '../models/Product';
|
||||
@@ -11,6 +13,7 @@ export class StockFillDB extends Dexie {
|
||||
areas!: Table<Area>;
|
||||
pickLists!: Table<PickList>;
|
||||
pickItems!: Table<PickItem>;
|
||||
categories!: Table<Category>;
|
||||
|
||||
constructor() {
|
||||
super('stockfill');
|
||||
@@ -21,6 +24,33 @@ export class StockFillDB extends Dexie {
|
||||
pickLists: 'id, area_id, created_at, completed_at',
|
||||
pickItems: 'id, pick_list_id, product_id, status, created_at, updated_at',
|
||||
});
|
||||
this.version(2)
|
||||
.stores({
|
||||
products:
|
||||
'id, name, category, barcode, archived, created_at, updated_at',
|
||||
areas: 'id, name, created_at, updated_at',
|
||||
pickLists: 'id, area_id, created_at, completed_at',
|
||||
pickItems: 'id, pick_list_id, product_id, status, created_at, updated_at',
|
||||
categories: 'id, name, created_at, updated_at',
|
||||
})
|
||||
.upgrade(async (tx) => {
|
||||
const existingCategories = await tx.table('categories').count();
|
||||
if (existingCategories > 0) return;
|
||||
|
||||
const products = await tx.table('products').toArray();
|
||||
const uniqueCategories = Array.from(new Set(products.map((product) => product.category)));
|
||||
if (uniqueCategories.length === 0) return;
|
||||
|
||||
const now = Date.now();
|
||||
await tx.table('categories').bulkAdd(
|
||||
uniqueCategories.map((name: string) => ({
|
||||
id: uuidv4(),
|
||||
name,
|
||||
created_at: now,
|
||||
updated_at: now,
|
||||
})),
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -13,6 +13,16 @@ export const seedDatabase = async (db: StockFillDB) => {
|
||||
]);
|
||||
}
|
||||
|
||||
const categoryCount = await db.categories.count();
|
||||
if (categoryCount === 0) {
|
||||
await db.categories.bulkAdd([
|
||||
{ id: uuidv4(), name: 'Drinks', created_at: now(), updated_at: now() },
|
||||
{ id: uuidv4(), name: 'Snacks', created_at: now(), updated_at: now() },
|
||||
{ id: uuidv4(), name: 'Dairy', created_at: now(), updated_at: now() },
|
||||
{ id: uuidv4(), name: 'Confectionery', created_at: now(), updated_at: now() },
|
||||
]);
|
||||
}
|
||||
|
||||
const productCount = await db.products.count();
|
||||
if (productCount === 0) {
|
||||
await db.products.bulkAdd([
|
||||
|
||||
Reference in new issue
Block a user