Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a find-or-create macro #89

Open
fractaledmind opened this issue Sep 13, 2023 · 0 comments
Open

Add a find-or-create macro #89

fractaledmind opened this issue Sep 13, 2023 · 0 comments
Labels
enhancement New feature or request

Comments

@fractaledmind
Copy link
Owner

Imagine a scenario like this. We have a workflow where we need the author of a Slack message. Maybe a webhook has already processed and created our internal database representation, maybe it hasn't. So, in our job, we need to

  1. try and find the internal record
  2. if we don't have it, make an API call to fetch the data
  3. if the API read succeeds, create the internal record
  4. find the newly created internal record and set the variable

for example:

def find_author
  self.slack_author = event.installation.profiles.find_by(uid: @user_uid)
end

def fetch_unknown_author_from_slack
  return [] if @slack_author

  api_response = event.installation.client.user(uid: @user_uid)
  if api_response[:ok]
    [Slack::ProcessProfileSlackJob.with(event.installation, api_response[:user])]
  else
    raise DoNotRetryJob
  end
end

def ensure_author_known
   self.slack_author ||= event.installation.profiles.find_by(uid: @user_uid)
  raise DoNotRetryJob and return if @slack_author.nil?
end

Should this be something that the DSL makes more straight-forward?

@fractaledmind fractaledmind added the enhancement New feature or request label Sep 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant