buyer data

This commit is contained in:
root
2025-12-21 11:40:06 +01:00
parent 4686aa1ef3
commit fc8af8f96b
3 changed files with 86 additions and 10 deletions

View File

@@ -3,7 +3,7 @@
-- https://www.phpmyadmin.net/
--
-- Host: localhost:3306
-- Generation Time: Dec 20, 2025 at 05:20 PM
-- Generation Time: Dec 21, 2025 at 09:44 AM
-- Server version: 10.11.14-MariaDB-0+deb12u2
-- PHP Version: 8.2.29
@@ -31,7 +31,22 @@ CREATE TABLE `buyers` (
`id` int(11) NOT NULL,
`username` varchar(255) NOT NULL,
`password` varchar(255) NOT NULL,
`email` varchar(255) NOT NULL
`email` varchar(255) NOT NULL,
`created_at` datetime NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
-- --------------------------------------------------------
--
-- Table structure for table `buyer_data`
--
CREATE TABLE `buyer_data` (
`id` int(11) NOT NULL,
`buyer_id` int(11) NOT NULL,
`fullname` text NOT NULL,
`address` text NOT NULL,
`phone` varchar(15) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
-- --------------------------------------------------------
@@ -61,7 +76,8 @@ CREATE TABLE `drops` (
`unit` varchar(12) NOT NULL DEFAULT 'g',
`image_url` varchar(255) DEFAULT NULL,
`ppu` int(11) NOT NULL DEFAULT 1,
`created_at` datetime NOT NULL DEFAULT current_timestamp()
`created_at` datetime NOT NULL DEFAULT current_timestamp(),
`start_time` datetime DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
-- --------------------------------------------------------
@@ -76,10 +92,24 @@ CREATE TABLE `pending_orders` (
`order_id` varchar(255) NOT NULL,
`drop_id` int(11) NOT NULL,
`buyer_id` int(11) NOT NULL,
`buyer_data_id` int(11) NOT NULL,
`size` int(11) NOT NULL,
`price_amount` decimal(10,2) NOT NULL,
`price_currency` varchar(10) NOT NULL DEFAULT 'chf',
`created_at` datetime NOT NULL DEFAULT current_timestamp()
`created_at` datetime NOT NULL DEFAULT current_timestamp(),
`expires_at` datetime NOT NULL DEFAULT (current_timestamp() + interval 10 minute)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
-- --------------------------------------------------------
--
-- Table structure for table `referrals`
--
CREATE TABLE `referrals` (
`id` int(11) NOT NULL,
`referrer` int(11) NOT NULL,
`referree` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
-- --------------------------------------------------------
@@ -92,6 +122,7 @@ CREATE TABLE `sales` (
`id` int(11) NOT NULL,
`drop_id` int(11) NOT NULL,
`buyer_id` int(11) NOT NULL,
`buyer_data_id` int(11) NOT NULL,
`size` int(11) NOT NULL DEFAULT 1,
`payment_id` text NOT NULL DEFAULT '',
`created_at` datetime NOT NULL DEFAULT current_timestamp()
@@ -107,6 +138,12 @@ CREATE TABLE `sales` (
ALTER TABLE `buyers`
ADD PRIMARY KEY (`id`);
--
-- Indexes for table `buyer_data`
--
ALTER TABLE `buyer_data`
ADD PRIMARY KEY (`id`);
--
-- Indexes for table `deliveries`
--
@@ -128,7 +165,17 @@ ALTER TABLE `pending_orders`
ADD UNIQUE KEY `payment_id` (`payment_id`),
ADD UNIQUE KEY `order_id` (`order_id`),
ADD KEY `drop_id` (`drop_id`),
ADD KEY `buyer_id` (`buyer_id`);
ADD KEY `buyer_id` (`buyer_id`),
ADD KEY `idx_expires_at` (`expires_at`),
ADD KEY `buyer_data_id` (`buyer_data_id`);
--
-- Indexes for table `referrals`
--
ALTER TABLE `referrals`
ADD PRIMARY KEY (`id`),
ADD KEY `referree` (`referree`),
ADD KEY `referrer` (`referrer`);
--
-- Indexes for table `sales`
@@ -136,7 +183,8 @@ ALTER TABLE `pending_orders`
ALTER TABLE `sales`
ADD PRIMARY KEY (`id`),
ADD KEY `drop_id` (`drop_id`),
ADD KEY `buyer_id` (`buyer_id`);
ADD KEY `buyer_id` (`buyer_id`),
ADD KEY `buyer_data_id` (`buyer_data_id`);
--
-- AUTO_INCREMENT for dumped tables
@@ -148,6 +196,12 @@ ALTER TABLE `sales`
ALTER TABLE `buyers`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;
--
-- AUTO_INCREMENT for table `buyer_data`
--
ALTER TABLE `buyer_data`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;
--
-- AUTO_INCREMENT for table `deliveries`
--
@@ -166,6 +220,12 @@ ALTER TABLE `drops`
ALTER TABLE `pending_orders`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;
--
-- AUTO_INCREMENT for table `referrals`
--
ALTER TABLE `referrals`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;
--
-- AUTO_INCREMENT for table `sales`
--
@@ -187,14 +247,23 @@ ALTER TABLE `deliveries`
--
ALTER TABLE `pending_orders`
ADD CONSTRAINT `pending_orders_ibfk_1` FOREIGN KEY (`drop_id`) REFERENCES `drops` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
ADD CONSTRAINT `pending_orders_ibfk_2` FOREIGN KEY (`buyer_id`) REFERENCES `buyers` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;
ADD CONSTRAINT `pending_orders_ibfk_2` FOREIGN KEY (`buyer_id`) REFERENCES `buyers` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
ADD CONSTRAINT `pending_orders_ibfk_3` FOREIGN KEY (`buyer_data_id`) REFERENCES `buyer_data` (`id`);
--
-- Constraints for table `referrals`
--
ALTER TABLE `referrals`
ADD CONSTRAINT `referrals_ibfk_1` FOREIGN KEY (`referree`) REFERENCES `buyers` (`id`),
ADD CONSTRAINT `referrals_ibfk_2` FOREIGN KEY (`referrer`) REFERENCES `buyers` (`id`);
--
-- Constraints for table `sales`
--
ALTER TABLE `sales`
ADD CONSTRAINT `sales_ibfk_1` FOREIGN KEY (`drop_id`) REFERENCES `drops` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
ADD CONSTRAINT `sales_ibfk_2` FOREIGN KEY (`buyer_id`) REFERENCES `buyers` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;
ADD CONSTRAINT `sales_ibfk_2` FOREIGN KEY (`buyer_id`) REFERENCES `buyers` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
ADD CONSTRAINT `sales_ibfk_3` FOREIGN KEY (`buyer_data_id`) REFERENCES `buyer_data` (`id`);
COMMIT;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;

View File

@@ -215,10 +215,11 @@ export async function deletePendingOrderByOrderId(orderId: string): Promise<bool
export async function createSaleFromPendingOrder(pendingOrder: PendingOrder, paymentId: string): Promise<Sale> {
try {
const [result] = await pool.execute(
'INSERT INTO sales (drop_id, buyer_id, size, payment_id, created_at) VALUES (?, ?, ?, ?, NOW())',
'INSERT INTO sales (drop_id, buyer_id, buyer_data_id, size, payment_id, created_at) VALUES (?, ?, ?, ?, ?, NOW())',
[
pendingOrder.drop_id,
pendingOrder.buyer_id,
pendingOrder.buyer_data_id,
pendingOrder.size,
paymentId
]
@@ -352,6 +353,9 @@ export async function movePaymentToSalesWithInventoryCheck(
if (pendingOrder.buyer_id === undefined || pendingOrder.buyer_id === null) {
throw new Error(`Pending order missing buyer_id for id: ${pendingOrderId}`);
}
if (pendingOrder.buyer_data_id === undefined || pendingOrder.buyer_data_id === null) {
throw new Error(`Pending order missing buyer_data_id for id: ${pendingOrderId}`);
}
if (pendingOrder.size === undefined || pendingOrder.size === null) {
throw new Error(`Pending order missing size for id: ${pendingOrderId}`);
}
@@ -361,10 +365,11 @@ export async function movePaymentToSalesWithInventoryCheck(
// Create sale record (Step 5 from guide)
const [insertResult] = await connection.execute(
'INSERT INTO sales (drop_id, buyer_id, size, payment_id, created_at) VALUES (?, ?, ?, ?, NOW())',
'INSERT INTO sales (drop_id, buyer_id, buyer_data_id, size, payment_id, created_at) VALUES (?, ?, ?, ?, ?, NOW())',
[
pendingOrder.drop_id,
pendingOrder.buyer_id,
pendingOrder.buyer_data_id,
pendingOrder.size,
paymentId
]

View File

@@ -8,6 +8,7 @@ export interface PendingOrder {
order_id: string;
drop_id: number;
buyer_id: number;
buyer_data_id: number;
size: number;
price_amount: number;
price_currency: string;
@@ -19,6 +20,7 @@ export interface Sale {
id: number;
drop_id: number;
buyer_id: number;
buyer_data_id: number;
size: number;
payment_id: string;
created_at: Date;