added 260907 version of worker toolkit

This commit is contained in:
2026-09-08 20:30:51 -04:00
parent cbd3f0f8ca
commit 97aca37663
1283 changed files with 142951 additions and 0 deletions

View File

@@ -0,0 +1,12 @@
module Api
module V1
class ChartSerializer < ApplicationSerializer
attributes :id,
:start_at,
:end_at
has_many :checkins, embed: :ids, embed_in_root: true
has_many :trackables, embed: :objects
end
end
end

View File

@@ -0,0 +1,7 @@
module Api
module V1
class CheckinConditionSerializer < CheckinTrackableSerializer
attributes :condition_id
end
end
end

View File

@@ -0,0 +1,55 @@
module Api
module V1
class CheckinSerializer < ApplicationSerializer
attributes :id, :date, :note, :tag_ids, :food_ids, :postal_code, :available_for_hbi?, :available_for_promotion?,
:location_name, :promotion_skipped_at
has_many :conditions, embed: :objects, serializer: CheckinConditionSerializer
has_many :symptoms, embed: :objects, serializer: CheckinSymptomSerializer
has_many :treatments, embed: :objects, serializer: CheckinTreatmentSerializer
has_one :weather, embed_in_root: true
has_many :tags, embed_in_root: true
has_many :foods, embed_in_root: true
has_one :harvey_bradshaw_index, embed_in_root: true
has_one :promotion_rate, embed_in_root: true
def harvey_bradshaw_index
object.harvey_bradshaw_index if show_single_relation?(:harveyBradshawIndices)
end
def promotion_rate
object.promotion_rate if show_single_relation?(:promotionRate)
end
def weather
object.weather if show_single_relation?(:weathersMeasures)
end
def location_name
object.position&.location_name
end
def postal_code
object.position&.postal_code
end
%w[condition symptom treatment].each do |name|
plural_name = name.pluralize
define_method(plural_name.to_sym) do
query = object.includes ? {"#{name}_id" => {"$in": (object.includes[plural_name] || []).map(&:to_i)}} : {}
object.send(plural_name).where(query)
end
end
private
def show_single_relation?(key)
!object.includes || (object.includes && object.includes[key].present?)
end
end
end
end

View File

@@ -0,0 +1,7 @@
module Api
module V1
class CheckinSymptomSerializer < CheckinTrackableSerializer
attributes :symptom_id
end
end
end

View File

@@ -0,0 +1,7 @@
module Api
module V1
class CheckinTrackableSerializer < ApplicationSerializer
attributes :checkin_id, :value, :color_id, :position
end
end
end

View File

@@ -0,0 +1,14 @@
module Api
module V1
class CheckinTreatmentSerializer < CheckinTrackableSerializer
attributes :treatment_id, :is_taken
has_one :dose, embed: :object
private
def dose
::Dose.new(name: object.value) if object.value.present?
end
end
end
end

View File

@@ -0,0 +1,18 @@
module Api
module V1
class CommentSerializer < ApplicationSerializer
include Concerns::Notificatable
include Concerns::ReactionRelatable
attributes :post_id, :body, :user_name, :postable_id, :type
def type
"comment"
end
def postable_id
:fake
end
end
end
end

View File

@@ -0,0 +1,6 @@
module Api
module V1
module Concerns
end
end
end

View File

@@ -0,0 +1,18 @@
module Api
module V1
module Concerns
module Notificatable
extend ActiveSupport::Concern
included do
attributes :notifications
end
def notifications
return {} unless current_user
object.notifications.where(encrypted_notify_user_id: current_user.encrypted_id).count_by_types
end
end
end
end
end

View File

@@ -0,0 +1,21 @@
module Api
module V1
module Concerns
module ReactionRelatable
extend ActiveSupport::Concern
included do
attributes :reactions
end
def reactions
ReactionSerializer.new(
object.reactions.values_count_with_participated(current_user&.encrypted_id),
object.id,
object.class.name
)
end
end
end
end
end

View File

@@ -0,0 +1,18 @@
module Api
module V1
module Concerns
module TopicSerializable
extend ActiveSupport::Concern
included do
attributes :id, :tag_ids, :symptom_ids, :condition_ids, :treatment_ids
has_many :tags, embed_in_root: true
has_many :symptoms, embed_in_root: true
has_many :conditions, embed_in_root: true
has_many :treatments, embed_in_root: true
end
end
end
end
end

View File

@@ -0,0 +1,21 @@
# == Schema Information
#
# Table name: conditions
#
# id :integer not null, primary key
# global :boolean default(TRUE)
# created_at :datetime not null
# updated_at :datetime not null
# trackable_usages_count :integer default(0)
#
module Api
module V1
class ConditionSerializer < ApplicationSerializer
include SearchableSerializer
include TrackableSerializer
attributes :id, :name
end
end
end

View File

@@ -0,0 +1,15 @@
module Api
module V1
class CountrySerializer < ApplicationSerializer
attributes :id, :name
def id
object.alpha2
end
def name
object.translation(I18n.locale.to_s)
end
end
end
end

View File

@@ -0,0 +1,7 @@
module Api
module V1
class DayHabitSerializer < RankedEnumSerializer
attributes :description
end
end
end

View File

@@ -0,0 +1,9 @@
module Api
module V1
class DoseSerializer < ApplicationSerializer
include SearchableSerializer
attributes :id, :name
end
end
end

View File

@@ -0,0 +1,6 @@
module Api
module V1
class EducationLevelSerializer < RankedEnumSerializer
end
end
end

View File

@@ -0,0 +1,6 @@
module Api
module V1
class EthnicitySerializer < RankedEnumSerializer
end
end
end

View File

@@ -0,0 +1,9 @@
module Api
module V1
class FoodSerializer < ApplicationSerializer
include SearchableSerializer
attributes :id, :name
end
end
end

View File

@@ -0,0 +1,8 @@
module Api
module V1
class HarveyBradshawIndexSerializer < ApplicationSerializer
attributes :id, :abdominal_mass, :abdominal_pain, :abscess, :anal_fissure, :aphthous_ulcers, :arthralgia, :score,
:erythema_nodosum, :new_fistula, :pyoderma_gangrenosum, :stools, :uveitis, :well_being, :checkin_id
end
end
end

View File

@@ -0,0 +1,9 @@
module Api
module V1
class InvitationSerializer < ApplicationSerializer
attributes :id, :email
has_one :user, embed_in_root: true
end
end
end

View File

@@ -0,0 +1,14 @@
module Api
module V1
class OracleRequestSerializer < ApplicationSerializer
attributes :id, :age, :sex_id, :symptom_ids, :responce, :can_edit
has_one :sex, embed_in_root: true
has_many :symptoms, embed_in_root: true
def can_edit
object.can_edit?(scope)
end
end
end
end

View File

@@ -0,0 +1,13 @@
module Api
module V1
class OracleRequestWithTokenSerializer < OracleRequestSerializer
self.root = :oracle_request
attributes :token
def can_edit
true
end
end
end
end

View File

@@ -0,0 +1,32 @@
module Api
module V1
class PasswordSerializer < ApplicationSerializer
attributes :id, :email, :reset_password_token, :current_password, :password, :password_confirmation
def id
if reset_password_token.present?
reset_password_token
else
Digest::SHA256.base64digest(DateTime.current.to_s)
end
end
def email
object.try :email
end
def reset_password_token
serialization_options[:token]
end
def password
end
def password_confirmation
end
def current_password
end
end
end
end

View File

@@ -0,0 +1,11 @@
module Api
module V1
class PatternSerializer < ApplicationSerializer
attributes :id, :start_at, :end_at, :name, :includes, :author_name
def author_name
object.author.screen_name
end
end
end
end

View File

@@ -0,0 +1,21 @@
module Api
module V1
class PostSerializer < ApplicationSerializer
include Concerns::Notificatable
include Concerns::TopicSerializable
include Concerns::ReactionRelatable
attributes :id, :body, :title, :type, :user_name, :comments_count, :postable_id, :priority
has_many :comments, embed: :ids
def type
"post"
end
def priority
object.try(:priority)
end
end
end
end

View File

@@ -0,0 +1,60 @@
module Api
module V1
class PostableSerializer
FAKE_ID = "fake".freeze
attr_reader :postables, :tag_ids, :symptom_ids, :condition_ids, :treatment_ids, :postable_post_ids, :current_user
def initialize(postables, current_user)
@postables = postables
@current_user = current_user
end
def as_json(_opts = {})
posts = []
comments = []
postables.each { |postable| (postable.is_a?(Post) ? posts : comments) << postable }
prepare_ids(posts)
posts.concat(Post.where(:id.in => comments.map(&:post_id) - posts.map(&:id)))
{
postables: [{
id: FAKE_ID,
post_ids: postable_post_ids,
comment_ids: comments.map { |o| o.id.to_s }
}],
tags: ActiveModel::ArraySerializer.new(Tag.where(id: tag_ids), {}),
posts: ActiveModel::ArraySerializer.new(posts, scope: current_user),
comments: ActiveModel::ArraySerializer.new(comments, scope: current_user),
conditions: ActiveModel::ArraySerializer.new(Condition.where(id: condition_ids), {}),
symptoms: ActiveModel::ArraySerializer.new(Symptom.where(id: symptom_ids), {}),
treatments: ActiveModel::ArraySerializer.new(Treatment.where(id: treatment_ids), {})
}
end
private
def prepare_ids(posts)
@tag_ids = []
@symptom_ids = []
@condition_ids = []
@treatment_ids = []
@postable_post_ids = []
posts.each do |post|
@postable_post_ids << post.id.to_s
@tag_ids.concat(post.tag_ids)
@symptom_ids.concat(post.symptom_ids)
@condition_ids.concat(post.condition_ids)
@treatment_ids.concat(post.treatment_ids)
post.postable_id = FAKE_ID
end
end
end
end
end

View File

@@ -0,0 +1,50 @@
# == Schema Information
#
# Table name: profiles
#
# id :integer not null, primary key
# user_id :integer
# country_id :string
# birth_date :date
# sex_id :string
# onboarding_step_id :string
# created_at :datetime not null
# updated_at :datetime not null
# ethnicity_ids_string :string
# day_habit_id :string
# education_level_id :string
# day_walking_hours :integer
# most_recent_doses :hstore
# screen_name :string
# most_recent_conditions_positions :hstore
# most_recent_symptoms_positions :hstore
# most_recent_treatments_positions :hstore
#
module Api
module V1
class ProfileSerializer < ApplicationSerializer
attributes :id, :screen_name, :birth_date, :country_id, :sex_id, :onboarding_step_id,
:ethnicity_ids, :day_habit_id, :education_level_id, :day_walking_hours,
:pressure_units, :temperature_units, :beta_tester, :notify_token, :notify, :checkin_reminder,
:checkin_reminder_at, :time_zone_name, :notify_top_posts
def checkin_reminder_at
{hours: serialized_time[0], minutes: serialized_time[1]}
end
def time_zone_name
time_zone_name = object.time_zone_name
return Profile::TIMEZONE_PARAMS[:time_zone_name] if time_zone_name.nil? || time_zone_name&.empty?
time_zone_name
end
def serialized_time
reminder_at = object.checkin_reminder_at
reminder_at.blank? ? Profile::TIMEZONE_PARAMS[:time] : reminder_at.strftime("%H:%M").split(":")
end
end
end
end

View File

@@ -0,0 +1,7 @@
module Api
module V1
class PromotionRateSerializer < ApplicationSerializer
attributes :id, :score, :feedback, :checkin_id
end
end
end

View File

@@ -0,0 +1,7 @@
module Api
module V1
class RankedEnumSerializer < ApplicationSerializer
attributes :id, :name, :rank
end
end
end

View File

@@ -0,0 +1,29 @@
module Api
module V1
class ReactionSerializer
attr_reader :reactions, :reactable_id, :reactable_type
def initialize(reactions, reactable_id, reactable_type)
@reactions = reactions
@reactable_id = reactable_id
@reactable_type = reactable_type
end
def as_json(_opts = {})
reactions.map { |reaction| serialized_reaction(reaction) }
end
def serialize_one
{
reaction: serialized_reaction(reactions.first)
}
end
private
def serialized_reaction(raw_reaction)
raw_reaction.merge(reactable_id: reactable_id, reactable_type: reactable_type)
end
end
end
end

View File

@@ -0,0 +1,13 @@
module Api
module V1
class RegistrationSerializer < ApplicationSerializer
has_one :user, embed_in_root: true
private
def id
object.user.id
end
end
end
end

View File

@@ -0,0 +1,9 @@
module Api
module V1
class SearchSerializer < ApplicationSerializer
attributes :id
has_many :searchables, embed: :objects
end
end
end

View File

@@ -0,0 +1,15 @@
module Api
module V1
module SearchableSerializer
extend ActiveSupport::Concern
included do
attributes :type
end
def type
object.class.name.demodulize.downcase.dasherize
end
end
end
end

View File

@@ -0,0 +1,37 @@
module Api
module V1
class SessionSerializer < ApplicationSerializer
attributes :id,
:user_id,
:email,
:token,
:settings
def id
1
end
def user_id
object.try :id
end
def email
object.try :email
end
def token
object.try :authentication_token
end
def settings
{
base_url: ENV["BASE_URL"],
notification_channel: object.try(:notification_channel),
facebook_app_id: ENV["FACEBOOK_APP_ID"],
discourse_url: Flaredown.config.discourse_url,
discourse_enabled: Flaredown.config.discourse_enabled?
}
end
end
end
end

View File

@@ -0,0 +1,6 @@
module Api
module V1
class SexSerializer < RankedEnumSerializer
end
end
end

View File

@@ -0,0 +1,21 @@
# == Schema Information
#
# Table name: symptoms
#
# id :integer not null, primary key
# global :boolean default(TRUE)
# created_at :datetime not null
# updated_at :datetime not null
# trackable_usages_count :integer default(0)
#
module Api
module V1
class SymptomSerializer < ApplicationSerializer
include SearchableSerializer
include TrackableSerializer
attributes :id, :name
end
end
end

View File

@@ -0,0 +1,21 @@
# == Schema Information
#
# Table name: tags
#
# id :integer not null, primary key
# created_at :datetime not null
# updated_at :datetime not null
#
module Api
module V1
class TagSerializer < ApplicationSerializer
include SearchableSerializer
attributes :id, :name, :users_count
def users_count
object.trackable_usages_count
end
end
end
end

View File

@@ -0,0 +1,7 @@
module Api
module V1
class TopicFollowingSerializer < ApplicationSerializer
include Concerns::TopicSerializable
end
end
end

View File

@@ -0,0 +1,23 @@
module Api
module V1
module TrackableSerializer
extend ActiveSupport::Concern
included do
attributes :color_id, :type, :users_count
end
def color_id
@color_id ||= Tracking.find_by(user: current_user, trackable: object).try :color_id
end
def type
object.class.name.downcase.dasherize
end
def users_count
object.trackable_usages_count
end
end
end
end

View File

@@ -0,0 +1,22 @@
# == Schema Information
#
# Table name: trackings
#
# id :integer not null, primary key
# user_id :integer
# trackable_id :integer
# trackable_type :string
# start_at :date
# end_at :date
# color_id :integer
# created_at :datetime not null
# updated_at :datetime not null
#
module Api
module V1
class TrackingSerializer < ApplicationSerializer
attributes :id, :user_id, :trackable_id, :trackable_type, :start_at, :end_at, :color_id
end
end
end

View File

@@ -0,0 +1,21 @@
# == Schema Information
#
# Table name: treatments
#
# id :integer not null, primary key
# global :boolean default(TRUE)
# created_at :datetime not null
# updated_at :datetime not null
# trackable_usages_count :integer default(0)
#
module Api
module V1
class TreatmentSerializer < ApplicationSerializer
include SearchableSerializer
include TrackableSerializer
attributes :id, :name
end
end
end

View File

@@ -0,0 +1,44 @@
# == Schema Information
#
# Table name: users
#
# id :integer not null, primary key
# email :string default(""), not null
# encrypted_password :string default(""), not null
# reset_password_token :string
# reset_password_sent_at :datetime
# remember_created_at :datetime
# sign_in_count :integer default(0), not null
# current_sign_in_at :datetime
# last_sign_in_at :datetime
# current_sign_in_ip :string
# last_sign_in_ip :string
# authentication_token :string not null
# invitation_token :string
# invitation_created_at :datetime
# invitation_sent_at :datetime
# invitation_accepted_at :datetime
# invitation_limit :integer
# invited_by_id :integer
# invited_by_type :string
# created_at :datetime not null
# updated_at :datetime not null
#
module Api
module V1
class UserSerializer < ApplicationSerializer
attributes :id, :email, :intercom_hash, :topic_following_id
has_one :profile, embed_in_root: true
def topic_following_id
object.topic_following.id
end
def intercom_hash
OpenSSL::HMAC.hexdigest("sha256", ENV["INTERCOM_SECRET"], object.email)
end
end
end
end

View File

@@ -0,0 +1,7 @@
module Api
module V1
class WeatherSerializer < ApplicationSerializer
attributes :id, :humidity, :icon, :precip_intensity, :pressure, :summary, :temperature_max, :temperature_min
end
end
end

View File

@@ -0,0 +1,19 @@
class ApplicationSerializer < ActiveModel::Serializer
include Rails.application.routes.url_helpers
attributes :id, :created_at, :updated_at
alias_method :current_user, :scope
def id
object.try :id
end
def created_at
object.try :created_at
end
def updated_at
object.try :updated_at
end
end