BryanJBryce

BryanJBryce

Resources involved are Employee, Shift, and EmployeeShift

Employee and shift are related many_to_many, through EmployeeShift, however, since an employee can clock in and out multiple times during an shift EmployeeShift has it’s own id and isn’t unique by pairs of Employee and Shift ids:

  attributes do
    uuid_primary_key :id
    attribute :in_at, :utc_datetime_usec, allow_nil?: false
    attribute :out_at, :utc_datetime_usec

    create_timestamp :created_at
    update_timestamp :updated_at
  end

  relationships do
    belongs_to :employee, PointOfSale.Accounts.Employee do
      api PointOfSale.Accounts
      allow_nil? false
      attribute_writable? true
    end

    belongs_to :shift, PointOfSale.Stores.Shift do
      allow_nil? false
      attribute_writable? true
    end
  end

Additionally EmployeeShifts have a custom primary create action called start that takes arguments of employ_id and shift_id and set’s the start time:

create :start do
      primary? true
      accept [:employee_id, :shift_id]

      argument :employee_id, :uuid, allow_nil?: false
      argument :shift_id, :uuid, allow_nil?: false

      change set_attribute(:in_at, DateTime.utc_now())
      change set_attribute(:employee_id, arg(:employee_id))
      change set_attribute(:shift_id, arg(:shift_id))
    end

When I start a new Shift I want to be able to pass an array of Employee ids to manage_relationship so that the custom start create function is called on EmployeeShift so that the included employees are clocked in automatically as the shift starts:

Shift


    has_many :employee_shifts, PointOfSale.Stores.EmployeeShift

    many_to_many :employees, PointOfSale.Accounts.Employee do
      api PointOfSale.Accounts
      through PointOfSale.Stores.EmployeeShift
      join_relationship :employee_shifts
    end
create :start do
      accept [:till_id, :employee_ids]

      argument :till_id, :uuid, allow_nil?: false
      argument :employee_ids, {:array, :uuid}, allow_nil?: false

      change set_attribute(:start_at, DateTime.utc_now())
      change set_attribute(:till_id, arg(:till_id))
      change manage_relationship(:employee_ids, :employee_shifts, type: create)
    end

However, when I attempt to create a new Shift via this action I get an error saying that the required arguments of shift_id and employee_id for EmployeeShift are missing.

The shift_id should come from the shift that is created just before running the manage relationship. And I thought I would be accepting employee_ids via the manage_relationship function.

I see that I can pass a map instead of a list of ids, and maybe that is the way, but that doesn’t solve the passing in of the shift_id that I don’t have since it get’s created.

Showing Posts 1 to 10

zachdaniel

zachdaniel

Creator of Ash

Hmm…this rings a bell on a bug that was fixed in the not too distant past. How up to date is your ash version?

BryanJBryce

BryanJBryce OP

OK, updated everything, still no luck:

iex(4)> shift = Shift.start!(%{till_id: till.id, employee_ids: employee_ids}, tenant: "dreambean")
[debug] QUERY OK db=0.2ms idle=1367.8ms
begin []
↳ anonymous fn/3 in Ash.Changeset.with_hooks/3, at: lib/ash/changeset/changeset.ex:2186
[debug] QUERY OK source="shifts" db=3.5ms
INSERT INTO "dreambean"."shifts" ("id","created_at","updated_at","start_at","till_id") VALUES ($1,$2,$3,$4,$5) RETURNING "till_id","updated_at","created_at","end_at","start_at","id" ["e3758301-5c59-4b8b-8fbc-ae80f258b281", ~U[2024-02-08 16:44:03.213427Z], ~U[2024-02-08 16:44:03.213427Z], ~U[2024-02-08 16:29:33Z], "9f5d6205-70f0-43ca-8a74-dc5de13b7886"]
↳ AshPostgres.DataLayer.bulk_create/3, at: lib/data_layer.ex:1412
[debug] QUERY OK db=0.4ms
rollback []
↳ anonymous fn/3 in Ash.Changeset.with_hooks/3, at: lib/ash/changeset/changeset.ex:2186
** (Ash.Error.Invalid) Input Invalid

* argument employee_id is required    at employee_ids, 0

* argument shift_id is required    at employee_ids, 0

    (ash 2.18.2) lib/ash/api/api.ex:2711: Ash.Api.unwrap_or_raise!/3
    iex:4: (file)
zachdaniel

zachdaniel

Creator of Ash

ah, okay interesting. Sorry, didn’t notice this until now.

Try this:

      change manage_relationship(:employee_ids, :employee_shifts, type: :create, value_is_key: :employee_id)
BryanJBryce

BryanJBryce OP

That solves the employee_id :tada:

But not the shift_id

** (Ash.Error.Invalid) Input Invalid

* argument shift_id is required    at employee_ids, 0

    (ash 2.18.2) lib/ash/api/api.ex:2711: Ash.Api.unwrap_or_raise!/3
    iex:4: (file)
BryanJBryce

BryanJBryce OP

I’m wondering if I even need the many_to_many at this point because the join table now has its own attributes and id, so should it really be modeled that way? I’m going to see if it changes anything. I don’t expect it to since the function is operating on the has_many relationship.

BryanJBryce

BryanJBryce OP

Yeah, that didn’t change it. I wonder if it is because EmployeeShift doesn’t use shift_id or employee_id as primary keys.

BryanJBryce

BryanJBryce OP

Same result.

zachdaniel

zachdaniel

Creator of Ash

Ah, okay I think its because its using the primary create action on the join resource which you’ve configured with required arguments.

zachdaniel

zachdaniel

Creator of Ash

You’ll need to point it at a different create action and/or make those arguments optional (because manage_relationship writes to the attributes, it doesn’t set arguments).

BryanJBryce

BryanJBryce OP

Ah, I see, lemme throw in new action in there. Does it always use the primary action or is there a way to specify?

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
rahultumpala
Hello, I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews