Skip to content

Commit

Permalink
Merge pull request #61 from AII-the-time/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
raipen authored Oct 19, 2023
2 parents 274191b + 7007e73 commit a16fc76
Show file tree
Hide file tree
Showing 29 changed files with 2,272 additions and 84 deletions.
152 changes: 152 additions & 0 deletions prisma/menuSeed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
import * as fs from 'fs';
import * as path from 'path';
import { PrismaClient } from '@prisma/client';

const materialRawData = fs.readFileSync(path.join(process.cwd(),'prisma', '소예다방-재료.csv'), 'utf8').toString().trim();
const materialData = materialRawData.split('\n').slice(2).map((row) => {
const [name, amount, unit, price] = row.split(',');
return {
name,
amount: parseInt(amount),
unit,
price
};
});

const mixedMaterialRawData = fs.readFileSync(path.join(process.cwd(),'prisma', '소예다방-Mixed재료.csv'), 'utf8').toString().trim();
const mixedMaterialData = mixedMaterialRawData.split('\n').slice(2).join('\n').split('\n,,\n').map((material) => {
const rows = material.split('\n');
const name = rows[0].split(',')[0];
const materials = rows.map((row) => {
const [_,name, amount] = row.split(',');
return {
name,
amount
};
});
return { name, materials };
});

const menuRawData = fs.readFileSync(path.join(process.cwd(),'prisma', '소예다방-메뉴.csv'), 'utf8').toString().trim();
const menuData = menuRawData.split('\n').slice(3).join('\n').split('\n,,,,,,\n').map((menu) => {
const rows = menu.split('\n');
const name = rows[0].split(',')[0];
const price = rows[0].split(',')[1];
const materials = rows.map((row) => {
const [_,__,name, ...amounts] = row.split(',');
const [coldRegularAmount, coldSizeUpAmount, hotRegularAmount, hotSizeUpAmount] = amounts.map(Number);
return {
name,
coldRegularAmount,
coldSizeUpAmount,
hotRegularAmount,
hotSizeUpAmount
};
});
return { name, price, materials };
});

export const printAllStocks = () => {
const stocks = [...new Set([...new Set(menuData.flatMap((menu) => menu.materials.map((material) => material.name)))].flatMap((name) => {
const materials = mixedMaterialData.find((material) => material.name === name)?.materials;
if(!materials) return name;
return materials.map((material) => material.name);
}))];

console.log(stocks.join('\n'));
}

export default async (prisma: PrismaClient, storeId: number) => {
const materials = await Promise.all(materialData.map(async (material) => {
return await prisma.stock.create({
data: {
name: material.name,
amount: isNaN(material.amount)?undefined:material.amount,
unit: material.unit===""?undefined:material.unit,
price: material.price===""?undefined:material.price,
storeId
}
});
}));

const mixedMaterialDataWithMaterials = mixedMaterialData.map((material) => {
return {
name: material.name,
materials: material.materials.map((material) => {
const stock = materials.find((stock) => stock.name === material.name);
if(!stock) throw new Error(`Stock not found: ${material.name}`);
return {
name: stock.name,
amount: material.amount,
stockId: stock.id
};
})
};
});
const mixedMaterials = await Promise.all(mixedMaterialDataWithMaterials.map(async (material) => {
return await prisma.mixedStock.create({
data: {
name: material.name,
storeId,
mixings: {
create: material.materials.map((material) => ({
amount: parseInt(material.amount),
stockId: material.stockId,
}))
}
}
});
}));

const menuDataWithMaterials = menuData.map((menu) => {
return {
name: menu.name,
price: menu.price,
materials: menu.materials.map((material) => {
let stock:{id:number}|undefined= materials.find((stock) => stock.name === material.name);
if(!stock) {
stock = mixedMaterials.find((stock) => stock.name === material.name);
if(!stock) throw new Error(`Stock not found: ${material.name}`);
}
return {
name: material.name,
coldRegularAmount: material.coldRegularAmount,
coldSizeUpAmount: material.coldSizeUpAmount,
hotRegularAmount: material.hotRegularAmount,
hotSizeUpAmount: material.hotSizeUpAmount,
stockId: stock.id
};
})
};
});
const category3 = await prisma.category.create({
data: {
id: 3,
storeId,
name: '미분류',
sort: 3,
createdAt: new Date(),
updatedAt: new Date(),
},
});
const menus = await Promise.all(menuDataWithMaterials.map(async (menu,index) => {
return await prisma.menu.create({
data: {
name: menu.name,
price: menu.price,
categoryId: 3,
sort: index+1,
storeId,
recipes: {
create: menu.materials.map((material) => ({
coldRegularAmount: material.coldRegularAmount,
coldSizeUpAmount: material.coldSizeUpAmount,
hotRegularAmount: material.hotRegularAmount,
hotSizeUpAmount: material.hotSizeUpAmount,
stockId: material.stockId,
}))
}
}
});
}));
}
37 changes: 37 additions & 0 deletions prisma/migrations/20231011061113_init/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
-- CreateTable
CREATE TABLE `Recipe` (
`id` INTEGER NOT NULL AUTO_INCREMENT,
`storeId` INTEGER NOT NULL,
`menuId` INTEGER NOT NULL,
`stockId` INTEGER NOT NULL,
`coldRegularAmount` INTEGER NULL,
`coldSizeUpAmount` INTEGER NULL,
`hotRegularAmount` INTEGER NULL,
`hotSizeUpAmount` INTEGER NULL,

PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `Stock` (
`id` INTEGER NOT NULL AUTO_INCREMENT,
`storeId` INTEGER NOT NULL,
`name` VARCHAR(191) NOT NULL,
`standard` INTEGER NOT NULL,
`count` INTEGER NOT NULL,
`sort` INTEGER NOT NULL,

PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- AddForeignKey
ALTER TABLE `Recipe` ADD CONSTRAINT `Recipe_storeId_fkey` FOREIGN KEY (`storeId`) REFERENCES `Store`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `Recipe` ADD CONSTRAINT `Recipe_menuId_fkey` FOREIGN KEY (`menuId`) REFERENCES `Menu`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `Recipe` ADD CONSTRAINT `Recipe_stockId_fkey` FOREIGN KEY (`stockId`) REFERENCES `Stock`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `Stock` ADD CONSTRAINT `Stock_storeId_fkey` FOREIGN KEY (`storeId`) REFERENCES `Store`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;
61 changes: 61 additions & 0 deletions prisma/migrations/20231012124817_init/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
Warnings:
- You are about to drop the column `count` on the `Stock` table. All the data in the column will be lost.
- You are about to drop the column `sort` on the `Stock` table. All the data in the column will be lost.
- You are about to drop the column `standard` on the `Stock` table. All the data in the column will be lost.
*/
-- DropForeignKey
ALTER TABLE `Recipe` DROP FOREIGN KEY `Recipe_stockId_fkey`;

-- AlterTable
ALTER TABLE `Recipe` ADD COLUMN `mixedStockId` INTEGER NULL,
MODIFY `stockId` INTEGER NULL;

-- AlterTable
ALTER TABLE `Stock` DROP COLUMN `count`,
DROP COLUMN `sort`,
DROP COLUMN `standard`,
ADD COLUMN `amount` INTEGER NULL,
ADD COLUMN `unit` VARCHAR(191) NULL;

-- CreateTable
CREATE TABLE `MixedStock` (
`id` INTEGER NOT NULL AUTO_INCREMENT,
`storeId` INTEGER NOT NULL,
`name` VARCHAR(191) NOT NULL,
`totalAmount` INTEGER NULL,
`unit` VARCHAR(191) NULL,

PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `Mixing` (
`id` INTEGER NOT NULL AUTO_INCREMENT,
`storeId` INTEGER NOT NULL,
`mixedStockId` INTEGER NOT NULL,
`stockId` INTEGER NOT NULL,
`amount` INTEGER NOT NULL,

PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- AddForeignKey
ALTER TABLE `Recipe` ADD CONSTRAINT `Recipe_stockId_fkey` FOREIGN KEY (`stockId`) REFERENCES `Stock`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `Recipe` ADD CONSTRAINT `Recipe_mixedStockId_fkey` FOREIGN KEY (`mixedStockId`) REFERENCES `MixedStock`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `MixedStock` ADD CONSTRAINT `MixedStock_storeId_fkey` FOREIGN KEY (`storeId`) REFERENCES `Store`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `Mixing` ADD CONSTRAINT `Mixing_storeId_fkey` FOREIGN KEY (`storeId`) REFERENCES `Store`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `Mixing` ADD CONSTRAINT `Mixing_mixedStockId_fkey` FOREIGN KEY (`mixedStockId`) REFERENCES `MixedStock`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `Mixing` ADD CONSTRAINT `Mixing_stockId_fkey` FOREIGN KEY (`stockId`) REFERENCES `Stock`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;
2 changes: 2 additions & 0 deletions prisma/migrations/20231013122529_init/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE `Stock` ADD COLUMN `price` DECIMAL(65, 30) NULL;
21 changes: 21 additions & 0 deletions prisma/migrations/20231018133609_init/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
Warnings:
- You are about to drop the column `storeId` on the `Mixing` table. All the data in the column will be lost.
- You are about to drop the column `storeId` on the `Recipe` table. All the data in the column will be lost.
*/
-- DropForeignKey
ALTER TABLE `Mixing` DROP FOREIGN KEY `Mixing_storeId_fkey`;

-- DropForeignKey
ALTER TABLE `Recipe` DROP FOREIGN KEY `Recipe_storeId_fkey`;

-- AlterTable
ALTER TABLE `Mixing` DROP COLUMN `storeId`;

-- AlterTable
ALTER TABLE `Recipe` DROP COLUMN `storeId`;

-- AlterTable
ALTER TABLE `Stock` ADD COLUMN `currentAmount` INTEGER NOT NULL DEFAULT 0;
4 changes: 3 additions & 1 deletion prisma/seed.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Prisma, PrismaClient } from '@prisma/client';
import { PrismaClient } from '@prisma/client';
import menuSeed from './menuSeed';
const prisma = new PrismaClient();
async function main() {
const user = await prisma.user.create({
Expand Down Expand Up @@ -204,6 +205,7 @@ async function main() {
storeId: store.id,
},
});
await menuSeed(prisma, store.id);
}

main()
Expand Down
33 changes: 33 additions & 0 deletions prisma/소예다방-Mixed재료.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
원재료 레시피,,
,,
소스,생크림,485
,휘핑크림,485
,,
자몽 청,자몽 과육,600
,자몽 착즙,1000
,설탕,2000
,레몬즙,400
,구운소금,4
,레몬 제스트,8
,,
오렌지 청,오렌지 과육,600
,오렌지 착즙,1000
,설탕,2000
,레몬즙,400
,구운소금,4
,레몬 제스트,8
,,
블루베리 청,블루베리 과육,1000
,블루베리 착즙,600
,설탕,1800
,레몬즙,400
,구운소금,4
,레몬 제스트,8
,,
패션후르츠 청,패션후르츠 코디얼,2000
,설탕,2000
,구운소금,4
,레몬 제스트,8
,,
밀크티 베이스,무가당 홍차 베이스,1000
,가당 홍차 베이스,1000
Loading

0 comments on commit a16fc76

Please sign in to comment.