Skip to content

Commit

Permalink
Merge pull request #34 from rarimo/feat/daily_questions_rework
Browse files Browse the repository at this point in the history
Feat/daily questions rework
  • Loading branch information
artemskriabin authored Aug 27, 2024
2 parents d22b893 + 332f0fa commit d63b800
Show file tree
Hide file tree
Showing 36 changed files with 1,283 additions and 20 deletions.
11 changes: 11 additions & 0 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,17 @@ event_types:
short_description: Short description
auto_claim: true
no_auto_open: true
- name: daily_question
title: Get daily question
reward: 5
frequency: unlimited
description: This event is used when a user get daily question
short_description: event for get daily question
auto_claim: true
no_auto_open: true

daily_questions:
timezone: 4

levels:
downgradeable: false
Expand Down
15 changes: 15 additions & 0 deletions docs/spec/components/schemas/DailyQuestionAnswers.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
allOf:
- $ref: '#/components/schemas/DailyQuestionsKey'
- type: object
required:
- attributes
properties:
attributes:
type: object
required:
- answer
properties:
answer:
type: integer
format: int64
description: Selected/correct answer option
12 changes: 12 additions & 0 deletions docs/spec/components/schemas/DailyQuestionOptions.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
type: object
required:
- id
- title
properties:
id:
type: integer
format: int
description: Answer number for the question
title:
type: string
description: Answer text
35 changes: 35 additions & 0 deletions docs/spec/components/schemas/DailyQuestions.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
allOf:
- $ref: "#/components/schemas/DailyQuestionsKey"
- type: object
required:
- attributes
properties:
attributes:
type: object
required:
- title
- options
properties:
title:
type: string
description: Question title
example: Georgian capital
options:
type: array
description: Answer options. Minimum 2, maximum 6
items:
$ref: "#/components/schemas/DailyQuestionOptions"
example: [
{
"id": 0,
"title": ""
},
{
"id": 1,
"title": ""
},
{
"id": 2,
"title": ""
}
]
11 changes: 11 additions & 0 deletions docs/spec/components/schemas/DailyQuestionsKey.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
type: object
required:
- id
- type
properties:
id:
type: string
description: Question id
type:
type: string
enum: [ daily_questions ]
34 changes: 34 additions & 0 deletions docs/spec/components/schemas/DailyQuestionsStatus.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
allOf:
- $ref: "#/components/schemas/DailyQuestionsStatusKey"
- type: object
required:
- attributes
properties:
attributes:
type: object
required:
- next_question_date
- time_for_answer
- reward
properties:
next_question_date:
type: timestamp
format: int64
description: |
Time when the next question will be available.
If the time is in the past, then there is a question
on this day and the user has not yet answered it.
If the time is in the future, then the user has either
already answered the question on the current day or
there was no question on the current day.
example: 1725018539
time_for_answer:
type: integer
format: int64
description: The time within which the user has to answer this question after receiving it.
example: 30
reward:
type: integer
format: int64
description: The number of points the user will receive if they answer the question correctly.
example: 5
11 changes: 11 additions & 0 deletions docs/spec/components/schemas/DailyQuestionsStatusKey.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
type: object
required:
- id
- type
properties:
id:
type: string
description: Question id
type:
type: string
enum: [ daily_questions_status ]
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
get:
tags:
- Daily Questions
summary: Get daily question
description: |
Get a daily question. The user must be
authorized and verified (passport scanned,
verified field is true).
operationId: getDailyQuestion
security:
- BearerAuth: []
responses:
200:
description: Success
content:
application/vnd.api+json:
schema:
type: object
required:
- data
properties:
data:
$ref: '#/components/schemas/DailyQuestions'
400:
$ref: '#/components/responses/invalidParameter'
401:
$ref: '#/components/responses/invalidAuth'
403:
description: Questions unavailable, try later
content:
application/vnd.api+json:
schema:
$ref: '#/components/schemas/Errors'
404:
description: There is no question in current day.
content:
application/vnd.api+json:
schema:
$ref: '#/components/schemas/Errors'
409:
description: User already answer current day question.
content:
application/vnd.api+json:
schema:
$ref: '#/components/schemas/Errors'
500:
$ref: '#/components/responses/internalError'

post:
tags:
- Daily Questions
summary: Answer question
description: |
Answer question. The user must be
authorized and verified (passport scanned,
verified field is true).
operationId: answerDailyQuestion
security:
- BearerAuth: []
requestBody:
content:
application/vnd.api+json:
schema:
type: object
required:
- data
properties:
data:
$ref: '#/components/schemas/DailyQuestionAnswers'
responses:
200:
description: Success
content:
application/vnd.api+json:
schema:
type: object
required:
- data
properties:
data:
$ref: '#/components/schemas/DailyQuestionAnswers'
400:
$ref: '#/components/responses/invalidParameter'
401:
$ref: '#/components/responses/invalidAuth'
404:
description: User haven't active question or deadline already passed.
content:
application/vnd.api+json:
schema:
$ref: '#/components/schemas/Errors'
500:
$ref: '#/components/responses/internalError'
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
get:
tags:
- Daily Questions
summary: Daily question status
description: |
Get the status of questions. The user must be
authorized and verified (passport scanned,
verified field is true).
Returns NotFound if next question absent.
operationId: dailyQuestionsStatus
security:
- BearerAuth: []
responses:
200:
description: Success
content:
application/vnd.api+json:
schema:
type: object
required:
- data
properties:
data:
$ref: '#/components/schemas/DailyQuestionsStatus'
401:
$ref: '#/components/responses/invalidAuth'
404:
description: Next question not exist.
content:
application/vnd.api+json:
schema:
$ref: '#/components/schemas/Errors'
500:
$ref: '#/components/responses/internalError'
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ require (
gitlab.com/distributed_lab/logan v3.8.1+incompatible
gitlab.com/distributed_lab/running v1.6.0
gitlab.com/distributed_lab/urlval/v4 v4.0.3
gitlab.com/tokend/go v3.16.0+incompatible
)

require (
Expand Down Expand Up @@ -110,6 +111,7 @@ require (
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mmcloughlin/addchain v0.4.0 // indirect
github.com/mtibben/percent v0.2.1 // indirect
github.com/nullstyle/go-xdr v0.0.0-20180726165426-f4c839f75077 // indirect
github.com/oklog/ulid v1.3.1 // indirect
github.com/pelletier/go-toml/v2 v2.1.1 // indirect
github.com/petermattis/goid v0.0.0-20180202154549-b0b1615b78e5 // indirect
Expand Down
17 changes: 17 additions & 0 deletions internal/assets/migrations/004_daily_questions.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
-- +migrate Up
CREATE TABLE IF NOT EXISTS daily_questions (
id SERIAL PRIMARY KEY,
title VARCHAR(255) NOT NULL,
time_for_answer INTEGER NOT NULL,
reward INTEGER NOT NULL,
answer_options JSONB NOT NULL,
starts_at TIMESTAMP NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT (NOW() AT TIME ZONE 'utc'),
correct_answer INTEGER NOT NULL,
num_correct_answers INTEGER DEFAULT 0,
num_incorrect_answers INTEGER DEFAULT 0,
num_all_participants INTEGER DEFAULT 0
);

-- +migrate Down
DROP TABLE IF EXISTS daily_questions;
17 changes: 12 additions & 5 deletions internal/cli/workers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/rarimo/geo-points-svc/internal/config"
"github.com/rarimo/geo-points-svc/internal/data/evtypes"
"github.com/rarimo/geo-points-svc/internal/service"
"github.com/rarimo/geo-points-svc/internal/service/workers/cleanquestiondeadlines"
"github.com/rarimo/geo-points-svc/internal/service/workers/expirywatch"
"github.com/rarimo/geo-points-svc/internal/service/workers/leveljustice"
"github.com/rarimo/geo-points-svc/internal/service/workers/nooneisforgotten"
Expand All @@ -17,11 +18,12 @@ import (
func runServices(ctx context.Context, cfg config.Config, wg *sync.WaitGroup) {
// signals indicate the finished initialization of each worker
var (
reopenerSig = make(chan struct{})
expiryWatchSig = make(chan struct{})
evTypesSig = make(chan struct{})
noOneIsForgottenSig = make(chan struct{})
levelJustice = make(chan struct{})
reopenerSig = make(chan struct{})
expiryWatchSig = make(chan struct{})
evTypesSig = make(chan struct{})
noOneIsForgottenSig = make(chan struct{})
levelJustice = make(chan struct{})
cleanDQuestionDeadlines = make(chan struct{})
)

run := func(f func()) {
Expand All @@ -48,8 +50,13 @@ func runServices(ctx context.Context, cfg config.Config, wg *sync.WaitGroup) {
<-noOneIsForgottenSig
run(func() { leveljustice.Run(cfg, levelJustice) })

//service for cleaning daily question deadlines after day
run(func() { cleanquestiondeadlines.Run(ctx, cfg, cleanDQuestionDeadlines) })
<-cleanDQuestionDeadlines

// service depends on all the workers for good UX
<-expiryWatchSig
<-levelJustice
run(func() { service.Run(ctx, cfg) })

}
Loading

0 comments on commit d63b800

Please sign in to comment.