alerts: Teams webhook fan-out (contract 0.14.0) + printedparts detail revision column

send_webhook(title,text) posts alerts to an optional webhook (Teams Incoming
Webhook / Workflow, or generic JSON) via alert_webhook_url + alert_webhook_format
settings; send_alert fans out to it alongside email; exposed on shopdb.api
(0.13.0->0.14.0, PLUGIN-HOOKS synced); low-stock posts on its custom-recipient
path too. Also: recent-transactions table shows the consumed print-file revision.
This commit is contained in:
cproudlock
2026-07-22 10:21:42 -04:00
parent d141fef203
commit 38c7ec347b
9 changed files with 162 additions and 11 deletions

View File

@@ -330,26 +330,33 @@ def _send_lowstock_alert(item):
Recipients: Setting printedparts_alert_email (comma-separated), falling
back to the site's alert_recipients. Never fails the transaction - the
ledger write already committed."""
from shopdb.api import send_email, send_alert, Setting
from shopdb.api import send_email, send_alert, send_webhook, Setting
code = item.gagelabtag or item.itemcode
subject = (f'Low stock: {item.itemname} ({code}) - '
f'{item.quantityonhand} left')
# Absolute link to the item page when the site base URL is configured
# (e.g. https://<host>/ops). Emails have no request context to derive it.
base = (Setting.get('site_base_url') or '').rstrip('/')
link_html = (f'<p><a href="{base}/printedparts/{item.printeditemid}">'
f'View {code}</a></p>') if base else ''
item_url = f'{base}/printedparts/{item.printeditemid}' if base else ''
link_html = f'<p><a href="{item_url}">View {code}</a></p>' if item_url else ''
html = (f'<p><strong>{item.itemname}</strong> ({code}) is down '
f'to <strong>{item.quantityonhand}</strong> '
f'(threshold {item.lowstockthreshold}).</p>'
f'<p>Bin: {item.binlocation or "-"}</p>'
f'{link_html}'
f'<p>Time to print more.</p>')
webhook_text = (f'**{item.itemname}** ({code}) is down to '
f'{item.quantityonhand} (threshold {item.lowstockthreshold}). '
f'Bin: {item.binlocation or "-"}.'
+ (f' [View]({item_url})' if item_url else ''))
try:
recipients = _alert_recipients()
if recipients:
# send_email does not fan to the webhook, so post it explicitly.
send_webhook(subject, webhook_text)
send_email(recipients, subject, html)
else:
# send_alert already fans out to email + webhook.
send_alert(subject, html)
except Exception:
import logging

View File

@@ -120,6 +120,7 @@
<th>When</th>
<th>Type</th>
<th>Qty</th>
<th>Rev</th>
<th>Who</th>
<th>Reason</th>
</tr>
@@ -132,11 +133,12 @@
<td :class="transaction.quantitychange < 0 ? 'qty-out' : 'qty-in'">
{{ transaction.quantitychange > 0 ? '+' : '' }}{{ transaction.quantitychange }}
</td>
<td>{{ transaction.revision != null ? transaction.revision : '-' }}</td>
<td>{{ transaction.employeename || transaction.employeesso }}</td>
<td>{{ transaction.reason || '-' }}</td>
</tr>
<tr v-if="!item.recenttransactions?.length">
<td colspan="5" class="empty-state">No transactions yet</td>
<td colspan="6" class="empty-state">No transactions yet</td>
</tr>
</tbody>
</table>