[dyad] wrote 2 file(s)
This commit is contained in:
parent
4cefd74120
commit
883981a4be
2 changed files with 55 additions and 70 deletions
|
|
@ -1,77 +1,62 @@
|
|||
/// <reference path="../pb_data/types.d.ts" />
|
||||
|
||||
onRecordAfterCreateRequest((e) => {
|
||||
if (e.collection.name !== "book_out_records") {
|
||||
return;
|
||||
}
|
||||
|
||||
// It's generally safer to use onRecordAfterCreateSuccess to ensure the record is actually persisted
|
||||
// However, for immediate feedback or if you need to modify the response, onRecordAfterCreateRequest can be used.
|
||||
// For this example, we'll use onRecordAfterCreateSuccess to ensure data consistency.
|
||||
// The actual email sending logic will be in onRecordAfterCreateSuccess.
|
||||
// This hook is just to demonstrate where you might intercept the request.
|
||||
}, "book_out_records");
|
||||
// Existing hooks...
|
||||
|
||||
onRecordAfterCreateSuccess((e) => {
|
||||
if (e.collection.name !== "book_out_records") {
|
||||
return;
|
||||
}
|
||||
if (e.collection.name !== "purchase_orders") {
|
||||
return;
|
||||
}
|
||||
|
||||
const record = e.record;
|
||||
const record = e.record;
|
||||
|
||||
// Fetch related customer and item details
|
||||
$app.dao().findRecordById("customers", record.get("customer"))
|
||||
.then((customer) => {
|
||||
return $app.dao().findRecordById("stock_items", record.get("items"))
|
||||
.then((item) => {
|
||||
const customerName = customer.get("comapny_name");
|
||||
const itemName = item.get("name");
|
||||
const itemSku = item.get("sku");
|
||||
const quantity = record.get("quantity");
|
||||
const dateBookedOut = new Date(record.get("date_booked_out")).toLocaleString();
|
||||
// Define the recipient email for approval (replace with actual approver email)
|
||||
const approverEmail = "approver@example.com";
|
||||
|
||||
// Define the recipient email (e.g., an admin email or the customer's email if available)
|
||||
// IMPORTANT: Replace 'your-admin-email@example.com' with your actual admin email or a dynamic customer email
|
||||
const recipientEmail = 'your-admin-email@example.com';
|
||||
// Prepare order details for email
|
||||
const supplierName = record.get("supplierName");
|
||||
const orderDate = new Date(record.get("orderDate")).toLocaleString();
|
||||
const status = record.get("status");
|
||||
const items = record.get("items") || [];
|
||||
const quantities = record.get("quantities") || [];
|
||||
|
||||
const message = new MailerMessage({
|
||||
from: {
|
||||
address: $app.settings().meta.senderAddress,
|
||||
name: $app.settings().meta.senderName,
|
||||
},
|
||||
to: [{address: recipientEmail}],
|
||||
subject: `Stock Booked Out: ${itemName} (${itemSku})`,
|
||||
html: `
|
||||
<p>Dear Admin,</p>
|
||||
<p>The following item has been booked out:</p>
|
||||
<ul>
|
||||
<li><strong>Customer:</strong> ${customerName}</li>
|
||||
<li><strong>Item Name:</strong> ${itemName}</li>
|
||||
<li><strong>SKU:</strong> ${itemSku}</li>
|
||||
<li><strong>Quantity:</strong> ${quantity}</li>
|
||||
<li><strong>Date Booked Out:</strong> ${dateBookedOut}</li>
|
||||
</ul>
|
||||
<p>Thank you.</p>
|
||||
`,
|
||||
text: `
|
||||
Dear Admin,
|
||||
The following item has been booked out:
|
||||
Customer: ${customerName}
|
||||
Item Name: ${itemName}
|
||||
SKU: ${itemSku}
|
||||
Quantity: ${quantity}
|
||||
Date Booked Out: ${dateBookedOut}
|
||||
Thank you.
|
||||
`,
|
||||
});
|
||||
// Build HTML list of items and quantities
|
||||
let itemsListHtml = "<ul>";
|
||||
for (let i = 0; i < items.length; i++) {
|
||||
itemsListHtml += `<li>Item ID: ${items[i]}, Quantity: ${quantities[i]}</li>`;
|
||||
}
|
||||
itemsListHtml += "</ul>";
|
||||
|
||||
return $app.newMailClient().send(message);
|
||||
});
|
||||
})
|
||||
.then(() => {
|
||||
console.log("Book-out notification email sent successfully!");
|
||||
})
|
||||
.catch((err) => {
|
||||
console.error("Failed to send book-out notification email:", err);
|
||||
});
|
||||
}, "book_out_records");
|
||||
const message = new MailerMessage({
|
||||
from: {
|
||||
address: $app.settings().meta.senderAddress,
|
||||
name: $app.settings().meta.senderName,
|
||||
},
|
||||
to: [{ address: approverEmail }],
|
||||
subject: `Purchase Order Approval Request from ${supplierName}`,
|
||||
html: `
|
||||
<p>Dear Approver,</p>
|
||||
<p>A new purchase order has been created and requires your approval:</p>
|
||||
<p><strong>Supplier:</strong> ${supplierName}</p>
|
||||
<p><strong>Order Date:</strong> ${orderDate}</p>
|
||||
<p><strong>Status:</strong> ${status}</p>
|
||||
<p><strong>Items:</strong></p>
|
||||
${itemsListHtml}
|
||||
<p>Please review and approve or reject the order in the system.</p>
|
||||
<p>Thank you.</p>
|
||||
`,
|
||||
text: `
|
||||
Dear Approver,
|
||||
A new purchase order has been created and requires your approval:
|
||||
Supplier: ${supplierName}
|
||||
Order Date: ${orderDate}
|
||||
Status: ${status}
|
||||
Items:
|
||||
${itemsListHtml.replace(/<[^>]+>/g, '')}
|
||||
Please review and approve or reject the order in the system.
|
||||
Thank you.
|
||||
`,
|
||||
});
|
||||
|
||||
$app.newMailClient().send(message);
|
||||
console.log("Purchase order approval email sent.");
|
||||
});
|
||||
|
|
@ -20,7 +20,7 @@ import { showSuccess, showError } from "@/utils/toast"
|
|||
import pb from "@/lib/pocketbase"
|
||||
import { StockItem } from "@/components/stock/columns"
|
||||
|
||||
const statusOptions = ["Pending", "Ordered", "Received"] as const
|
||||
const statusOptions = ["Pending Approval", "Approved", "Rejected"] as const
|
||||
|
||||
const formSchema = z.object({
|
||||
supplierName: z.string().min(2, { message: "Supplier name must be at least 2 characters." }),
|
||||
|
|
@ -67,7 +67,7 @@ export function PurchaseOrderForm({ setOpen, refreshData }: PurchaseOrderFormPro
|
|||
resolver: zodResolver(formSchema),
|
||||
defaultValues: {
|
||||
supplierName: "",
|
||||
status: "Pending",
|
||||
status: "Pending Approval",
|
||||
items: [{ stockItemId: "", quantity: 1 }],
|
||||
},
|
||||
})
|
||||
|
|
|
|||
Loading…
Reference in a new issue