Skip to content

Commit

Permalink
refactor timex logic into its own utils module
Browse files Browse the repository at this point in the history
  • Loading branch information
jarlah committed Oct 3, 2024
1 parent 6d982e2 commit cddc452
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 26 deletions.
26 changes: 12 additions & 14 deletions lib/journi_plan/itineraries/projectors/activity.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,22 @@ defmodule JourniPlan.Itineraries.Projectors.Activity do

import JourniPlan.Utils.UUID

alias JourniPlan.Itineraries.Events.ActivityCreated
alias JourniPlan.Itineraries.Events.ActivityNameUpdated
alias JourniPlan.Itineraries.Events.ActivityDescriptionUpdated
alias JourniPlan.Itineraries.Events.ActivityStartTimeUpdated
alias JourniPlan.Itineraries.Events.ActivityEndTimeUpdated
alias JourniPlan.Itineraries.Events.ActivityDeleted
alias JourniPlan.Itineraries.Events.{
ActivityCreated,
ActivityNameUpdated,
ActivityDescriptionUpdated,
ActivityStartTimeUpdated,
ActivityEndTimeUpdated,
ActivityDeleted
}

alias JourniPlan.Itineraries.Projections.Activity

import JourniPlan.TimexUtils, only: [parse_to_datetime!: 1]

project(%ActivityCreated{} = created, _, fn multi ->
start_time =
created.start_time
|> Timex.parse!("%Y-%m-%dT%H:%M", :strftime)
|> Timex.to_datetime("Etc/UTC")
end_time =
created.end_time
|> Timex.parse!("%Y-%m-%dT%H:%M", :strftime)
|> Timex.to_datetime("Etc/UTC")
start_time = parse_to_datetime!(created.start_time)
end_time = parse_to_datetime!(created.end_time)
itinerary_uuid = cast_uuid!(created.itinerary_uuid)

Ecto.Multi.insert(multi, :activity, %Activity{
Expand Down
10 changes: 6 additions & 4 deletions lib/journi_plan/itineraries/projectors/itinerary.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ defmodule JourniPlan.Itineraries.Projectors.Itinerary do

alias JourniPlan.Repo

alias JourniPlan.Itineraries.Events.ItineraryCreated
alias JourniPlan.Itineraries.Events.ItineraryNameUpdated
alias JourniPlan.Itineraries.Events.ItineraryDescriptionUpdated
alias JourniPlan.Itineraries.Events.ItineraryDeleted
alias JourniPlan.Itineraries.Events.{
ItineraryCreated,
ItineraryNameUpdated,
ItineraryDescriptionUpdated,
ItineraryDeleted
}

alias JourniPlan.Itineraries.Projections.Itinerary

Expand Down
17 changes: 9 additions & 8 deletions lib/journi_plan/itineraries/projectors/journal_entry.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,19 @@ defmodule JourniPlan.Itineraries.Projectors.JournalEntry do

import JourniPlan.Utils.UUID

alias JourniPlan.Itineraries.Events.JournalEntryCreated
alias JourniPlan.Itineraries.Events.JournalEntryTitleUpdated
alias JourniPlan.Itineraries.Events.JournalEntryBodyUpdated
alias JourniPlan.Itineraries.Events.JournalEntryDeleted
alias JourniPlan.Itineraries.Events.{
JournalEntryCreated,
JournalEntryTitleUpdated,
JournalEntryBodyUpdated,
JournalEntryDeleted
}

alias JourniPlan.Itineraries.Projections.JournalEntry

import JourniPlan.TimexUtils, only: [parse_to_datetime!: 1]

project(%JournalEntryCreated{} = created, _, fn multi ->
entry_date =
created.entry_date
|> Timex.parse!("%Y-%m-%dT%H:%M", :strftime)
|> Timex.to_datetime("Etc/UTC")
entry_date = parse_to_datetime!(created.entry_date)
itinerary_uuid = cast_uuid!(created.itinerary_uuid)
activity_uuid = cast_uuid!(created.activity_uuid)

Expand Down
7 changes: 7 additions & 0 deletions lib/journi_plan/utils/timex.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
defmodule JourniPlan.TimexUtils do
def parse_to_datetime!(time) when is_binary(time) do
time
|> Timex.parse!("%Y-%m-%dT%H:%M", :strftime)
|> Timex.to_datetime("Etc/UTC")
end
end

0 comments on commit cddc452

Please sign in to comment.