-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #162 from joshsoftware/report-review-recognition-api
APIs for report and review a recognition
- Loading branch information
Showing
18 changed files
with
828 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package db | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
logger "github.com/sirupsen/logrus" | ||
) | ||
|
||
const ( | ||
createRecognitionModerationQuery = `INSERT INTO recognition_moderation (recognition_id, is_inappropriate, | ||
moderator_comment, moderated_by, moderated_at, created_at, updated_at) VALUES ($1, $2, $3, $4, $5, $6, $7) | ||
RETURNING id, recognition_id, is_inappropriate, moderator_comment, moderated_by, moderated_at, created_at, updated_at` | ||
) | ||
|
||
type RecognitionModeration struct { | ||
ID int64 `db:"id" json:"id"` | ||
RecognitionID int64 `db:"recognition_id" json:"recognition_id"` | ||
IsInappropriate *bool `db:"is_inappropriate" json:"is_inappropriate"` | ||
ModeratorComment string `db:"moderator_comment" json:"comment"` | ||
ModeratedBy int64 `db:"moderated_by" json:"moderated_by"` | ||
ModeratedAt int64 `db:"moderated_at" json:"moderated_at"` | ||
CreatedAt time.Time `db:"created_at" json:"-"` | ||
UpdatedAt time.Time `db:"updated_at" json:"-"` | ||
} | ||
|
||
func (recognitionModeration *RecognitionModeration) Validate() (valid bool, errFields map[string]string) { | ||
errFields = make(map[string]string) | ||
|
||
if recognitionModeration.IsInappropriate == nil { | ||
errFields["is_inappropriate"] = "Can't be blank" | ||
} | ||
|
||
if len(errFields) == 0 { | ||
valid = true | ||
} | ||
return | ||
} | ||
|
||
func (s *pgStore) CreateRecognitionModeration(ctx context.Context, recognitionID int64, recognitionModeration RecognitionModeration) (resp RecognitionModeration, err error) { | ||
now := time.Now() | ||
err = s.db.GetContext( | ||
ctx, | ||
&resp, | ||
createRecognitionModerationQuery, | ||
recognitionID, | ||
recognitionModeration.IsInappropriate, | ||
recognitionModeration.ModeratorComment, | ||
recognitionModeration.ModeratedBy, | ||
now.Unix(), | ||
now, | ||
now, | ||
) | ||
if err != nil { | ||
logger.WithFields(logger.Fields{ | ||
"err": err.Error(), | ||
"recognitionID": recognitionID, | ||
"recognition_moderation_params": recognitionModeration, | ||
}).Error("Error while creating recognition moderation") | ||
return | ||
} | ||
|
||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package db | ||
|
||
import ( | ||
"context" | ||
"github.com/DATA-DOG/go-sqlmock" | ||
"github.com/jmoiron/sqlx" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/suite" | ||
"time" | ||
) | ||
|
||
// Define the suite, and absorb the built-in basic suite | ||
// functionality from testify - including assertion methods. | ||
type RecognitionModerationTestSuite struct { | ||
suite.Suite | ||
dbStore Storer | ||
db *sqlx.DB | ||
sqlmock sqlmock.Sqlmock | ||
} | ||
|
||
func (suite *RecognitionModerationTestSuite) SetupTest() { | ||
dbStore, dbConn, sqlmock := InitMockDB() | ||
suite.dbStore = dbStore | ||
suite.db = dbConn | ||
suite.sqlmock = sqlmock | ||
now = time.Now() | ||
mockedRows = suite.getMockedRows(now) | ||
} | ||
|
||
func (suite *RecognitionModerationTestSuite) TearDownTest() { | ||
suite.db.Close() | ||
} | ||
|
||
func (suite *RecognitionModerationTestSuite) getMockedRows(now time.Time) (mockedRows *sqlmock.Rows) { | ||
mockedRows = suite.sqlmock.NewRows([]string{"id", "recognition_id", "is_inappropriate", "moderator_comment", "moderated_by", "moderated_at", "created_at", "updated_at"}). | ||
AddRow(1, 1, true, "Test Comment", 1, now.Unix(), now, now) | ||
return | ||
} | ||
|
||
func (suite *RecognitionModerationTestSuite) TestCreateRecognitionModerationSuccess() { | ||
isInappropriate := true | ||
expectedRecognitionModeration := RecognitionModeration{ | ||
ID: int64(1), | ||
RecognitionID: int64(1), | ||
IsInappropriate: &isInappropriate, | ||
ModeratorComment: "Test Comment", | ||
ModeratedBy: int64(1), | ||
ModeratedAt: now.Unix(), | ||
CreatedAt: now, | ||
UpdatedAt: now, | ||
} | ||
|
||
suite.sqlmock.ExpectQuery("INSERT INTO recognition_moderation"). | ||
WithArgs(1, true, "Test Comment", 1, now.Unix(), sqlmock.AnyArg(), sqlmock.AnyArg()). | ||
WillReturnRows(mockedRows) | ||
|
||
resp, err := suite.dbStore.CreateRecognitionModeration(context.Background(), expectedRecognitionModeration.ID, expectedRecognitionModeration) | ||
|
||
assert.Equal(suite.T(), expectedRecognitionModeration, resp) | ||
assert.Nil(suite.T(), err) | ||
} | ||
|
||
func (suite *RecognitionModerationTestSuite) TestCreateRecognitionModerationFailure() { | ||
suite.db.Close() | ||
|
||
isInappropriate := true | ||
expectedRecognitionModeration := RecognitionModeration{ | ||
ID: int64(1), | ||
RecognitionID: int64(1), | ||
IsInappropriate: &isInappropriate, | ||
ModeratorComment: "Test Comment", | ||
ModeratedBy: int64(1), | ||
ModeratedAt: now.Unix(), | ||
CreatedAt: now, | ||
UpdatedAt: now, | ||
} | ||
|
||
suite.sqlmock.ExpectQuery("INSERT INTO recognition_moderation"). | ||
WithArgs(1, true, "Test Comment", 1, now.Unix(), sqlmock.AnyArg(), sqlmock.AnyArg()). | ||
WillReturnRows(mockedRows) | ||
|
||
resp, err := suite.dbStore.CreateRecognitionModeration(context.Background(), expectedRecognitionModeration.ID, expectedRecognitionModeration) | ||
|
||
assert.Equal(suite.T(), RecognitionModeration{}, resp) | ||
assert.NotNil(suite.T(), err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package db | ||
|
||
import ( | ||
"context" | ||
"strings" | ||
"time" | ||
|
||
logger "github.com/sirupsen/logrus" | ||
) | ||
|
||
var REPORTED_RECOGNITION_TYPE = []string{"fraud", "not_relevant", "incorrect"} | ||
|
||
const ( | ||
createReportedRecognitionQuery = `INSERT INTO reported_recognitions (recognition_id, type_of_reporting, | ||
reason_for_reporting, reported_by, reported_at, created_at, updated_at) VALUES ($1, $2, $3, $4, $5, $6, $7) | ||
RETURNING id, recognition_id, type_of_reporting, reason_for_reporting, reported_by, reported_at, created_at, updated_at` | ||
) | ||
|
||
type ReportedRecognition struct { | ||
ID int64 `db:"id" json:"id"` | ||
RecognitionID int64 `db:"recognition_id" json:"recognition_id"` | ||
TypeOfReporting string `db:"type_of_reporting" json:"mark_as"` | ||
ReasonForReporting string `db:"reason_for_reporting" json:"reason"` | ||
ReportedBy int64 `db:"reported_by" json:"reported_by"` | ||
ReportedAt int64 `db:"reported_at" json:"reported_at"` | ||
CreatedAt time.Time `db:"created_at" json:"-"` | ||
UpdatedAt time.Time `db:"updated_at" json:"-"` | ||
} | ||
|
||
func include(slice []string, val string) bool { | ||
for _, item := range slice { | ||
if item == val { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func (reportedRecognition *ReportedRecognition) Validate() (valid bool, errFields map[string]string) { | ||
errFields = make(map[string]string) | ||
|
||
if reportedRecognition.TypeOfReporting == "" { | ||
errFields["mark_as"] = "Can't be blank" | ||
} else { | ||
reportedRecognition.TypeOfReporting = strings.ToLower(reportedRecognition.TypeOfReporting) | ||
ok := include(REPORTED_RECOGNITION_TYPE, reportedRecognition.TypeOfReporting) | ||
if !ok { | ||
errFields["mark_as"] = "Invalid reported recognition type" | ||
} | ||
} | ||
|
||
if reportedRecognition.ReasonForReporting == "" { | ||
errFields["reason"] = "Can't be blank" | ||
} | ||
|
||
if len(errFields) == 0 { | ||
valid = true | ||
} | ||
return | ||
} | ||
|
||
func (s *pgStore) CreateReportedRecognition(ctx context.Context, recognitionID int64, reportedRecognition ReportedRecognition) (resp ReportedRecognition, err error) { | ||
now := time.Now() | ||
err = s.db.GetContext( | ||
ctx, | ||
&resp, | ||
createReportedRecognitionQuery, | ||
recognitionID, | ||
reportedRecognition.TypeOfReporting, | ||
reportedRecognition.ReasonForReporting, | ||
reportedRecognition.ReportedBy, | ||
now.Unix(), | ||
now, | ||
now, | ||
) | ||
if err != nil { | ||
logger.WithFields(logger.Fields{ | ||
"err": err.Error(), | ||
"recognitionID": recognitionID, | ||
"reported_recognition_params": reportedRecognition, | ||
}).Error("Error while creating reported recognition") | ||
return | ||
} | ||
|
||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package db | ||
|
||
import ( | ||
"context" | ||
"github.com/DATA-DOG/go-sqlmock" | ||
"github.com/jmoiron/sqlx" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/suite" | ||
"time" | ||
) | ||
|
||
// Define the suite, and absorb the built-in basic suite | ||
// functionality from testify - including assertion methods. | ||
type ReportedRecognitionTestSuite struct { | ||
suite.Suite | ||
dbStore Storer | ||
db *sqlx.DB | ||
sqlmock sqlmock.Sqlmock | ||
} | ||
|
||
func (suite *ReportedRecognitionTestSuite) SetupTest() { | ||
dbStore, dbConn, sqlmock := InitMockDB() | ||
suite.dbStore = dbStore | ||
suite.db = dbConn | ||
suite.sqlmock = sqlmock | ||
now = time.Now() | ||
mockedRows = suite.getMockedRows() | ||
} | ||
|
||
func (suite *ReportedRecognitionTestSuite) TearDownTest() { | ||
suite.db.Close() | ||
} | ||
|
||
func (suite *ReportedRecognitionTestSuite) getMockedRows() (mockedRows *sqlmock.Rows) { | ||
mockedRows = suite.sqlmock.NewRows([]string{"id", "recognition_id", "type_of_reporting", "reason_for_reporting", "reported_by", "reported_at", "created_at", "updated_at"}). | ||
AddRow(1, 1, "fraud", "Test Reason", 1, now.Unix(), now, now) | ||
return | ||
} | ||
|
||
func (suite *ReportedRecognitionTestSuite) TestCreateReportedRecognitionSuccess() { | ||
expectedReportedRecognition := ReportedRecognition{ | ||
ID: int64(1), | ||
RecognitionID: int64(1), | ||
TypeOfReporting: "fraud", | ||
ReasonForReporting: "Test Reason", | ||
ReportedBy: int64(1), | ||
ReportedAt: now.Unix(), | ||
CreatedAt: now, | ||
UpdatedAt: now, | ||
} | ||
|
||
suite.sqlmock.ExpectQuery("INSERT INTO reported_recognitions"). | ||
WithArgs(1, "fraud", "Test Reason", 1, now.Unix(), sqlmock.AnyArg(), sqlmock.AnyArg()). | ||
WillReturnRows(mockedRows) | ||
|
||
resp, err := suite.dbStore.CreateReportedRecognition(context.Background(), expectedReportedRecognition.ID, expectedReportedRecognition) | ||
|
||
assert.Equal(suite.T(), expectedReportedRecognition, resp) | ||
assert.Nil(suite.T(), err) | ||
} | ||
|
||
func (suite *ReportedRecognitionTestSuite) TestCreateReportedRecognitionFailure() { | ||
suite.db.Close() | ||
|
||
expectedReportedRecognition := ReportedRecognition{ | ||
ID: int64(1), | ||
RecognitionID: int64(1), | ||
TypeOfReporting: "fraud", | ||
ReasonForReporting: "Test Reason", | ||
ReportedBy: int64(1), | ||
ReportedAt: now.Unix(), | ||
CreatedAt: now, | ||
UpdatedAt: now, | ||
} | ||
|
||
suite.sqlmock.ExpectQuery("INSERT INTO reported_recognitions"). | ||
WithArgs(1, "fraud", "Test Reason", 1, now.Unix(), sqlmock.AnyArg(), sqlmock.AnyArg()). | ||
WillReturnRows(mockedRows) | ||
|
||
resp, err := suite.dbStore.CreateReportedRecognition(context.Background(), expectedReportedRecognition.ID, expectedReportedRecognition) | ||
|
||
assert.Equal(suite.T(), ReportedRecognition{}, resp) | ||
assert.NotNil(suite.T(), err) | ||
} |
1 change: 1 addition & 0 deletions
1
go-backend/migrations/1589893448_create_reported_recognitions.down.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
DROP TABLE reported_recognitions; |
10 changes: 10 additions & 0 deletions
10
go-backend/migrations/1589893448_create_reported_recognitions.up.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
CREATE TABLE reported_recognitions ( | ||
id SERIAL PRIMARY KEY NOT NULL UNIQUE, | ||
recognition_id integer REFERENCES recognitions(id), | ||
type_of_reporting varchar(50), | ||
reason_for_reporting TEXT, | ||
reported_by INTEGER REFERENCES users(id), | ||
reported_at BIGINT, | ||
created_at timestamp with time zone NOT NULL default current_timestamp, | ||
updated_at timestamp with time zone NOT NULL | ||
); |
1 change: 1 addition & 0 deletions
1
go-backend/migrations/1589962840_create_recognition_moderation.down.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
DROP TABLE recognition_moderation; |
Oops, something went wrong.