# Origamy playbook spec v1. Reference: /playbooks/spec-format/
spec_version: 1
id: retail.back-in-stock
title: Back in stock
sector: retail
source:
  kind: synthesized
  notes: >-
    No shop has built this with us yet. Written from the base patterns. Origamy can't look up
    who asked about a product when the stock changes, so the shop's system sends one event per
    waiting shopper when the item is restocked.
derived_from: null
vocabulary:
  person: shopper
  people: shoppers
  group: team
summary: >-
  Shoppers ask to be told when a sold-out item is back, through a small form or your store's
  own button. When it's restocked, each of them gets an email (and an SMS if they gave a
  number) with a link straight to the product.
when_to_use:
  - Items sell out and come back, and shoppers ask to be told.
  - Your store can send an event for each waiting shopper when an item is restocked.

requires:
  data_in:
    - how: public_form
      what: "The \"tell me when it's back\" form on sold-out product pages."
    - how: app_events
      what: "Your store sends one restock event per waiting shopper."
  channels:
    - channel: email
      for: The back-in-stock email
      needs: A verified sending domain for {{ params.email_from }}.
    - channel: sms
      for: The back-in-stock SMS (optional)
      needs: An SMS provider, or Origamy managed SMS after business verification (KYC).
  events:
    - onboarding.submitted
    - "{{ params.restock_event }}"
  traits:
    - waiting_for_product

parameters:
  - key: org_name
    ask: true
    question: What's your shop called?
    label: Shop name
    type: string
    example: Northwind Store
  - key: restock_event
    ask: true
    question: What will your store call the event it sends to each waiting shopper on restock?
    help: Properties product_name and product_url.
    label: Restock event
    type: event_name
    default: Back In Stock
  - key: use_form
    ask: true
    question: Do you want an Origamy form for "tell me when it's back", or does your store have its own button?
    label: Use an Origamy form
    type: boolean
    default: true
  - key: send_sms
    ask: true
    question: Send an SMS as well, when the shopper gave a number?
    label: SMS too
    type: boolean
    default: true
  - key: email_from
    ask: true
    question: Which address should emails come from?
    label: Sender address
    type: email
    example: shop@yourstore.example
  - key: greeting_fallback
    label: Greeting when a first name is missing
    type: string
    default: there

timings:
  - The email goes as soon as {{ params.restock_event }} arrives; the SMS 10 minutes later.
  - A shopper can be told about each product once (every event is a new product, so re-entry is allowed).

customer_must:
  - "When an item is restocked, have your store send {{ params.restock_event }} for each shopper who asked (with product_name and product_url), for example from the same job that updates stock."
  - If you use the form, publish it and link it from your sold-out product pages with ?product=<product id> in the link.
  - Verify the sending domain; for SMS, connect a provider or finish business verification.
  - Switch the follow-up on and test with your own email.

objects:
  - ref: form_notify
    kind: form
    when: { param: use_form, equals: true }
    about: '"Tell me when it''s back": email or phone, with the product id from the link.'
    body:
      name: Back in stock alert
      slug: back-in-stock
      description: Shoppers ask to be told when a sold-out product returns.
      draft_state:
        version: 1
        meta: { layout: classic, submit_label: Notify me, success_message: "Done! We'll tell you as soon as it's back." }
        pages:
          - id: p1
            title: Tell me when it's back
            description: Leave your email or phone and we'll let you know.
            fields:
              - { id: f_email, key: email, type: email, label: Email, required: true, maps_to: { mode: mapped, slot: profile.email } }
              - { id: f_phone, key: phone, type: phone, label: Phone (optional), required: false, maps_to: { mode: mapped, slot: profile.phone }, config: { country_picker: true, default_country: "{{ workspace.country }}", verify: "off" } }
              - { id: f_product, key: product, type: hidden, label: "", required: false, maps_to: { mode: custom, key: waiting_for_product, trait: true }, config: { param: product } }
      verification_settings: { required: false }
  - ref: tmpl_email
    kind: template
    about: The back-in-stock email.
    body:
      name: Back in stock (email)
      channel: email
      category: Stock
      style: html
      status: active
      subject: "It's back: {{ properties.product_name | default: \"the item you wanted\" }}"
      metadata: { email: { from: "{{ params.email_from }}", preview_text: "Get it before it sells out again." } }
      variables: {}
      body: >-
        <p>Hi {{ traits.first_name | default: "{{ params.greeting_fallback }}" }},</p>
        <p>Good news: {{ properties.product_name | default: "the item you asked about" }} is back
        in stock at {{ params.org_name }}.</p>
        <p><a href="{{ properties.product_url }}">See it now</a></p>
        <p style="font-size:12px;color:#6b6b80"><a href="{{ unsubscribe_url }}">Unsubscribe</a></p>
  - ref: tmpl_sms
    kind: template
    when: { param: send_sms, equals: true }
    about: The back-in-stock SMS.
    body:
      name: Back in stock (SMS)
      channel: sms
      category: Stock
      style: text
      status: active
      subject: ""
      metadata: {}
      variables: {}
      body: "{{ params.org_name }}: {{ properties.product_name | default: \"your item\" }} is back in stock. {{ properties.product_url }} Reply STOP to opt out."
  - ref: wf_restock
    kind: workflow
    about: Email (and SMS if they have a phone) when the restock event arrives.
    body:
      name: Back in stock
      description: Tells each waiting shopper when their product is back.
      enabled: false
      trigger_type: event
      trigger_config: { event_name: "{{ params.restock_event }}" }
      steps:
        - { id: s1, type: action, config: { action_type: send_message, channel_config: { mode: single, single: { channel: email, template_id: "{{ ref.tmpl_email.id }}" } } } }
        - { id: s2, when: { param: send_sms, equals: true }, type: delay, config: { duration: 10m } }
        - { id: s3, when: { param: send_sms, equals: true }, type: filter, config: { condition: { type: trait, trait_key: phone, cond_operator: exists } } }
        - { id: s4, when: { param: send_sms, equals: true }, type: action, config: { action_type: send_message, channel_config: { mode: single, single: { channel: sms, template_id: "{{ ref.tmpl_sms.id }}" } } } }
      re_entry_policy: every_time
      re_entry_cooldown_hours: 0
      goal: {}
      exit_conditions: []

eval:
  name: 71_retail_back_in_stock
  mode: task
  vertical: ""
  prompt: >-
    Shoppers can ask to be told when a sold-out product is back. When it's restocked, email
    them a link to the product, and text them too if they gave a phone number.
  expect_artifacts: [workflow, message_template]
  forbid_terms: []
