diff --git a/.env b/.env index 43bf5fd412feeda40517d5f5e722333b92271442..568639cff029d91346e8d347f2f5cbab64407d2e 100644 --- a/.env +++ b/.env @@ -4,6 +4,4 @@ PORT=8080 LOGTAIL_TOKEN= HTTP_TIMEOUT_SEC=2 LOG_FLUSH_INTERVAL_MS=1000 -SMTP_USERNAME="noreply@ocw.id" -SMTP_PORT=1025 FE_BASE_URL="http://localhost:3000" diff --git a/docs/docs.go b/docs/docs.go index 49b75f3556829fa1845dddc0737e82c9ab85bc01..5d2f69dab3a90c6f5fbbbbed2c6d98a1a11ee56c 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -1735,6 +1735,67 @@ const docTemplate = `{ } } }, + "/quiz/{id}/finish": { + "post": { + "description": "Finish quiz session and get the score", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "quiz" + ], + "summary": "Finish Quiz", + "parameters": [ + { + "type": "string", + "description": "Authenticate User (any role)", + "name": "Authorization", + "in": "header", + "required": true + }, + { + "description": "Quiz Finish payload", + "name": "data", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/quiz.FinishQuizPayload" + } + }, + { + "type": "string", + "format": "uuid", + "description": "Quiz id", + "name": "id", + "in": "path", + "required": true + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "allOf": [ + { + "$ref": "#/definitions/web.BaseResponse" + }, + { + "type": "object", + "properties": { + "data": { + "$ref": "#/definitions/quiz.QuizDetail" + } + } + } + ] + } + } + } + } + }, "/quiz/{id}/solution": { "get": { "description": "Take a quiz", @@ -1789,7 +1850,7 @@ const docTemplate = `{ }, "/quiz/{id}/take": { "post": { - "description": "Finish quiz session and get the score", + "description": "Take a quiz", "consumes": [ "application/json" ], @@ -1799,7 +1860,7 @@ const docTemplate = `{ "tags": [ "quiz" ], - "summary": "Finish Quiz", + "summary": "Take Quiz", "parameters": [ { "type": "string", @@ -1808,15 +1869,6 @@ const docTemplate = `{ "in": "header", "required": true }, - { - "description": "Quiz Finish payload", - "name": "data", - "in": "body", - "required": true, - "schema": { - "$ref": "#/definitions/quiz.FinishQuizPayload" - } - }, { "type": "string", "format": "uuid", diff --git a/docs/swagger.json b/docs/swagger.json index 883d3d799d994fab91eb3dbf9e1aee4d8483c36f..c610b2db132fe14ffbb7d539a60e92861377ce90 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -1727,6 +1727,67 @@ } } }, + "/quiz/{id}/finish": { + "post": { + "description": "Finish quiz session and get the score", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "quiz" + ], + "summary": "Finish Quiz", + "parameters": [ + { + "type": "string", + "description": "Authenticate User (any role)", + "name": "Authorization", + "in": "header", + "required": true + }, + { + "description": "Quiz Finish payload", + "name": "data", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/quiz.FinishQuizPayload" + } + }, + { + "type": "string", + "format": "uuid", + "description": "Quiz id", + "name": "id", + "in": "path", + "required": true + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "allOf": [ + { + "$ref": "#/definitions/web.BaseResponse" + }, + { + "type": "object", + "properties": { + "data": { + "$ref": "#/definitions/quiz.QuizDetail" + } + } + } + ] + } + } + } + } + }, "/quiz/{id}/solution": { "get": { "description": "Take a quiz", @@ -1781,7 +1842,7 @@ }, "/quiz/{id}/take": { "post": { - "description": "Finish quiz session and get the score", + "description": "Take a quiz", "consumes": [ "application/json" ], @@ -1791,7 +1852,7 @@ "tags": [ "quiz" ], - "summary": "Finish Quiz", + "summary": "Take Quiz", "parameters": [ { "type": "string", @@ -1800,15 +1861,6 @@ "in": "header", "required": true }, - { - "description": "Quiz Finish payload", - "name": "data", - "in": "body", - "required": true, - "schema": { - "$ref": "#/definitions/quiz.FinishQuizPayload" - } - }, { "type": "string", "format": "uuid", diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 58ea9348fb473426c5890cff161bd98360f62bf0..a2f545dc973227bf9562a6120358f9a35a4ddd09 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -1575,6 +1575,44 @@ paths: summary: Get Quiz Detail tags: - quiz + /quiz/{id}/finish: + post: + consumes: + - application/json + description: Finish quiz session and get the score + parameters: + - description: Authenticate User (any role) + in: header + name: Authorization + required: true + type: string + - description: Quiz Finish payload + in: body + name: data + required: true + schema: + $ref: '#/definitions/quiz.FinishQuizPayload' + - description: Quiz id + format: uuid + in: path + name: id + required: true + type: string + produces: + - application/json + responses: + "200": + description: OK + schema: + allOf: + - $ref: '#/definitions/web.BaseResponse' + - properties: + data: + $ref: '#/definitions/quiz.QuizDetail' + type: object + summary: Finish Quiz + tags: + - quiz /quiz/{id}/solution: get: consumes: @@ -1611,19 +1649,13 @@ paths: post: consumes: - application/json - description: Finish quiz session and get the score + description: Take a quiz parameters: - description: Authenticate User (any role) in: header name: Authorization required: true type: string - - description: Quiz Finish payload - in: body - name: data - required: true - schema: - $ref: '#/definitions/quiz.FinishQuizPayload' - description: Quiz id format: uuid in: path @@ -1642,7 +1674,7 @@ paths: data: $ref: '#/definitions/quiz.QuizDetail' type: object - summary: Finish Quiz + summary: Take Quiz tags: - quiz /reset/confirm: diff --git a/provider/redis/cache.go b/provider/redis/cache.go index 9d656ae2093191e41cdd8666e7f20f5b2c185a92..729c3e4f82cffd75685eb44160f5754695da93e9 100644 --- a/provider/redis/cache.go +++ b/provider/redis/cache.go @@ -43,23 +43,29 @@ func NewRedisEnv( IdleTimeout: 240 * time.Second, Dial: func() (redis.Conn, error) { defer resolver(log) - conn, err := redis.Dial("tcp", env.RedisConnection+":"+env.RedisPort) - if err != nil { - log.Warning("failed connect to redis server: tcp " + env.RedisConnection + ":" + env.RedisPort) - log.Warning(err.Error()) - - return nil, err + dialOptions := []redis.DialOption{ + redis.DialUseTLS(env.RedisUseTLS), } if env.RedisUseAuth { - if _, err := conn.Do("AUTH", env.RedisUsername, env.RedisPassword); err != nil { - conn.Close() + dialOptions = append(dialOptions, + redis.DialUsername(env.RedisUsername), + redis.DialPassword(env.RedisPassword), + ) + } - log.Warning("failed connect to redis server: authentication failed") + conn, err := redis.Dial( + "tcp", + env.RedisConnection+":"+env.RedisPort, + dialOptions..., + ) - return nil, err - } + if err != nil { + log.Warning("failed connect to redis server: tcp " + env.RedisConnection + ":" + env.RedisPort) + log.Warning(err.Error()) + + return nil, err } return conn, err diff --git a/service/auth/impl.go b/service/auth/impl.go index 9ac5f343fb68b92e44a3a51491389a21d5c70c5a..5175a0a02b2b13654f7f1328a131fb97447ea432 100644 --- a/service/auth/impl.go +++ b/service/auth/impl.go @@ -2,6 +2,7 @@ package auth import ( "gitlab.informatika.org/ocw/ocw-backend/repository/user" + "gitlab.informatika.org/ocw/ocw-backend/service/logger" "gitlab.informatika.org/ocw/ocw-backend/service/verification" "gitlab.informatika.org/ocw/ocw-backend/utils/env" "gitlab.informatika.org/ocw/ocw-backend/utils/password" @@ -14,4 +15,5 @@ type AuthServiceImpl struct { *env.Environment token.TokenUtil verification.VerificationService + logger.Logger } diff --git a/service/auth/register.go b/service/auth/register.go index 31734ec76daba39021ce75dbe480fed6d33bb3ef..5227bd811d8e9631c09d38f19d4cfef9c364fc45 100644 --- a/service/auth/register.go +++ b/service/auth/register.go @@ -24,12 +24,18 @@ func (auth AuthServiceImpl) Register(payload register.RegisterRequestPayload) er }) if errors.Is(err, gorm.ErrDuplicatedKey) { - auth.SendVerifyMail(payload.Email) + err := auth.SendVerifyMail(payload.Email) + if err != nil { + auth.Logger.Warning("Failed to send email: " + err.Error()) + } return nil } if err == nil { - auth.SendVerifyMail(payload.Email) + err := auth.SendVerifyMail(payload.Email) + if err != nil { + auth.Logger.Warning("Failed to send email: " + err.Error()) + } } return err diff --git a/test/mail/mail_test.go b/test/mail/mail_test.go index 59da881da5b07b1f12f4047e089820742e78a37d..f79df48b36a456633528bbdd997b7653c43cd884 100644 --- a/test/mail/mail_test.go +++ b/test/mail/mail_test.go @@ -8,13 +8,13 @@ import ( "gitlab.informatika.org/ocw/ocw-backend/utils/env" ) -func DisabledTestSendMail(t *testing.T) { +func TestSendMail(t *testing.T) { smtpClient := smtp.New(&env.Environment{ - SmtpUsername: "", - SmtpPassword: "", + SmtpUsername: "postmaster@bayusamudra.my.id", + SmtpPassword: "bfbd251bf296826f4c103646b45e4b64-70c38fed-ed2d2e67", SmtpIdentity: "", - SmtpServer: "", - SmtpPort: 21, + SmtpServer: "smtp.mailgun.org", + SmtpPort: 587, SmtpAuthType: "plain", }) diff --git a/utils/env/env.go b/utils/env/env.go index 428ffda96e91bf5dab99bcc5e78f0d0624a87692..85aa99986a2c0a45cb68f07927cbe954137a16e3 100644 --- a/utils/env/env.go +++ b/utils/env/env.go @@ -52,6 +52,7 @@ type Environment struct { RedisPassword string `env:"REDIS_PASSWORD"` RedisUseAuth bool `env:"REDIS_USE_AUTH" envDefault:"false"` RedisPrefixKey string `env:"REDIS_PREFIX_KEY" envDefault:"app:"` + RedisUseTLS bool `env:"REDIS_USE_TLS" envDefault:"false"` BucketEndpoint string `env:"BUCKET_ENDPOINT"` BucketSecretKey string `env:"BUCKET_SECRET_KEY"`