-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(backend/shared): save onboarding quiz answers to DB bb-167 (#227)
* feat(backend): add onboarding_answers_to_users table bb-167 * feat(backend): add onboarding answer entity bb-167 * feat(backend): add onboarding answer model bb-167 * feat(backend): add onboarding repository bb-167 * feat(backend): add onboarding controller bb-167 * feat(backend): add onboarding servise bb-167 * feat(backend): add question to answer model bb-167 * feat(backend): add onboarding question model bb-167 * feat(shared): add req/resp onboarding answer types bb-167 * feat(shared): add common quiz error extention bb-167 * feat(shared): add onboarding answers to user model bb-167 * refactor(backend): createUserAnswers repository bb-167 * refactor(backend): createUserAnswers in servise and controller bb-167 * feat(backend): add onboarding_answers_to_users to DB schema bb-167 * fix(backend): swagger for onboarding answer bb-167 * feat(backend): add findAnswersByIds to repo bb-167 * refactor(shared): remove unnecessary bb-167 * refactor(shared): rename onboardingError bb-167 * fix(backend): relation in user model bb-167 * refactor(backend): add findAnswersByIds and OnboardingAnswerResponseDto to service bb-167 * feat(shared): add validation to onboarding answers bb-167 * feat(shared): add OnboardingAnswerRequestBody bb-167 * feat(backend): create all methods for onboarding repository bb-167 * feat(backend): create all methods for onboarding service bb-167 * feat(backend): add userId to onboarding entity bb-167 * fix(backend): db schema bb-167 * fix(backend): add bracrs bb-167 * refactor(backend): imports, add braces bb-167 * refactor(backend): imports, add braces bb-167 * refactor(shared): rename OnboardingAnswerRequestBody bb-167 * refactor(shared/backend): move question length validation to service bb-167 * feat(backend): add onboarding answers length validation bb-167 * refactor(backend): use .count in countQuestions bb-167
- Loading branch information
1 parent
c5e20b4
commit 6779d9d
Showing
40 changed files
with
798 additions
and
5 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
apps/backend/src/db/migrations/20240827170622_onboarding_answers_to_users_table.ts
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,47 @@ | ||
import { type Knex } from "knex"; | ||
|
||
const TableName = { | ||
ONBOARDING_ANSWERS: "onboarding_answers", | ||
ONBOARDING_ANSWERS_TO_USERS: "onboarding_answers_to_users", | ||
USERS: "users", | ||
} as const; | ||
|
||
const ColumnName = { | ||
ANSWER_ID: "answer_id", | ||
CREATED_AT: "created_at", | ||
ID: "id", | ||
UPDATED_AT: "updated_at", | ||
USER_ID: "user_id", | ||
} as const; | ||
|
||
const DELETE_STRATEGY = "CASCADE"; | ||
|
||
function up(knex: Knex): Promise<void> { | ||
return knex.schema.createTable( | ||
TableName.ONBOARDING_ANSWERS_TO_USERS, | ||
(table) => { | ||
table.increments(ColumnName.ID).primary(); | ||
table | ||
.integer(ColumnName.USER_ID) | ||
.notNullable() | ||
.references(ColumnName.ID) | ||
.inTable(TableName.USERS) | ||
.onDelete(DELETE_STRATEGY); | ||
table | ||
.integer(ColumnName.ANSWER_ID) | ||
.notNullable() | ||
.references(ColumnName.ID) | ||
.inTable(TableName.ONBOARDING_ANSWERS) | ||
.onDelete(DELETE_STRATEGY); | ||
table.timestamp(ColumnName.CREATED_AT).defaultTo(knex.fn.now()); | ||
table.timestamp(ColumnName.UPDATED_AT).defaultTo(knex.fn.now()); | ||
table.unique([ColumnName.USER_ID, ColumnName.ANSWER_ID]); | ||
}, | ||
); | ||
} | ||
|
||
function down(knex: Knex): Promise<void> { | ||
return knex.schema.dropTableIfExists(TableName.ONBOARDING_ANSWERS_TO_USERS); | ||
} | ||
|
||
export { down, up }; |
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 |
---|---|---|
@@ -1,5 +1,8 @@ | ||
const RelationName = { | ||
ONBOARDING_ANSWERS: "answers", | ||
ONBOARDING_QUESTION: "question", | ||
USER_DETAILS: "userDetails", | ||
USERS: "users", | ||
} as const; | ||
|
||
export { RelationName }; |
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
3 changes: 3 additions & 0 deletions
3
apps/backend/src/libs/modules/database/libs/enums/database-table-name.enum.ts
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 |
---|---|---|
@@ -1,9 +1,9 @@ | ||
type Repository<T = unknown> = { | ||
create(payload: unknown): Promise<T>; | ||
delete(): Promise<boolean>; | ||
delete(id: number): Promise<boolean>; | ||
find(id: number): Promise<T>; | ||
findAll(): Promise<T[]>; | ||
update(): Promise<T>; | ||
update(id: number, payload: unknown): Promise<T>; | ||
}; | ||
|
||
export { type Repository }; |
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 |
---|---|---|
@@ -1,11 +1,11 @@ | ||
type Service<T = unknown> = { | ||
create(payload: unknown): Promise<T>; | ||
delete(): Promise<boolean>; | ||
delete(id: number): Promise<boolean>; | ||
find(id: number): Promise<T>; | ||
findAll(): Promise<{ | ||
items: T[]; | ||
}>; | ||
update(): Promise<T>; | ||
update(id: number, payload: unknown): Promise<T>; | ||
}; | ||
|
||
export { type Service }; |
4 changes: 4 additions & 0 deletions
4
apps/backend/src/modules/onboarding/libs/constants/constants.ts
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,4 @@ | ||
const DEFAULT_COUNT_VALUE = 0; | ||
const FIRST_ELEMENT_INDEX = 0; | ||
|
||
export { DEFAULT_COUNT_VALUE, FIRST_ELEMENT_INDEX }; |
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 @@ | ||
export { OnboardingApiPath } from "shared"; |
1 change: 1 addition & 0 deletions
1
apps/backend/src/modules/onboarding/libs/exceptions/exceptions.ts
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 @@ | ||
export { OnboardingError } from "shared"; |
5 changes: 5 additions & 0 deletions
5
apps/backend/src/modules/onboarding/libs/types/count-result.type.ts
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,5 @@ | ||
type CountResult = { | ||
count?: string; | ||
}; | ||
|
||
export { type CountResult }; |
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,7 @@ | ||
export { type CountResult } from "./count-result.type.js"; | ||
export { | ||
type OnboardingAnswerDto, | ||
type OnboardingAnswerRequestBodyDto, | ||
type OnboardingAnswerRequestDto, | ||
type OnboardingAnswerResponseDto, | ||
} from "shared"; |
1 change: 1 addition & 0 deletions
1
apps/backend/src/modules/onboarding/libs/validation-schemas/validation-schemas.ts
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 @@ | ||
export { onboardingAnswersValidationSchema } from "shared"; |
113 changes: 113 additions & 0 deletions
113
apps/backend/src/modules/onboarding/onboarding-answer.entity.ts
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,113 @@ | ||
import { type Entity } from "~/libs/types/types.js"; | ||
|
||
class OnboardingAnswerEntity implements Entity { | ||
private createdAt: string; | ||
|
||
private id: null | number; | ||
|
||
private label: string; | ||
|
||
private questionId: number; | ||
|
||
private updatedAt: string; | ||
|
||
private userId: null | number; | ||
|
||
private constructor({ | ||
createdAt, | ||
id, | ||
label, | ||
questionId, | ||
updatedAt, | ||
userId, | ||
}: { | ||
createdAt: string; | ||
id: null | number; | ||
label: string; | ||
questionId: number; | ||
updatedAt: string; | ||
userId: null | number; | ||
}) { | ||
this.createdAt = createdAt; | ||
this.id = id; | ||
this.label = label; | ||
this.questionId = questionId; | ||
this.updatedAt = updatedAt; | ||
this.userId = userId; | ||
} | ||
|
||
public static initialize({ | ||
createdAt, | ||
id, | ||
label, | ||
questionId, | ||
updatedAt, | ||
userId, | ||
}: { | ||
createdAt: string; | ||
id: number; | ||
label: string; | ||
questionId: number; | ||
updatedAt: string; | ||
userId: number; | ||
}): OnboardingAnswerEntity { | ||
return new OnboardingAnswerEntity({ | ||
createdAt, | ||
id, | ||
label, | ||
questionId, | ||
updatedAt, | ||
userId, | ||
}); | ||
} | ||
|
||
public static initializeNew({ | ||
label, | ||
questionId, | ||
}: { | ||
label: string; | ||
questionId: number; | ||
}): OnboardingAnswerEntity { | ||
return new OnboardingAnswerEntity({ | ||
createdAt: "", | ||
id: null, | ||
label, | ||
questionId, | ||
updatedAt: "", | ||
userId: null, | ||
}); | ||
} | ||
|
||
public toNewObject(): { | ||
createdAt: string; | ||
label: string; | ||
questionId: number; | ||
updatedAt: string; | ||
} { | ||
return { | ||
createdAt: this.createdAt, | ||
label: this.label, | ||
questionId: this.questionId, | ||
updatedAt: this.updatedAt, | ||
}; | ||
} | ||
public toObject(): { | ||
createdAt: string; | ||
id: number; | ||
label: string; | ||
questionId: number; | ||
updatedAt: string; | ||
userId: number; | ||
} { | ||
return { | ||
createdAt: this.createdAt, | ||
id: this.id as number, | ||
label: this.label, | ||
questionId: this.questionId, | ||
updatedAt: this.updatedAt, | ||
userId: this.userId as number, | ||
}; | ||
} | ||
} | ||
|
||
export { OnboardingAnswerEntity }; |
50 changes: 50 additions & 0 deletions
50
apps/backend/src/modules/onboarding/onboarding-answer.model.ts
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,50 @@ | ||
import { Model, type RelationMappings } from "objection"; | ||
|
||
import { | ||
AbstractModel, | ||
DatabaseTableName, | ||
} from "~/libs/modules/database/database.js"; | ||
|
||
import { UserModel } from "../users/users.js"; | ||
import { OnboardingQuestionModel } from "./onboarding-question.model.js"; | ||
|
||
class OnboardingAnswerModel extends AbstractModel { | ||
public label!: string; | ||
|
||
public questionId!: number; | ||
|
||
public userId!: number; | ||
|
||
public users!: UserModel[]; | ||
|
||
static get relationMappings(): RelationMappings { | ||
return { | ||
question: { | ||
join: { | ||
from: `${DatabaseTableName.ONBOARDING_ANSWERS}.questionId`, | ||
to: `${DatabaseTableName.ONBOARDING_QUESTIONS}.id`, | ||
}, | ||
modelClass: OnboardingQuestionModel, | ||
relation: Model.BelongsToOneRelation, | ||
}, | ||
users: { | ||
join: { | ||
from: `${DatabaseTableName.ONBOARDING_ANSWERS}.id`, | ||
through: { | ||
from: `${DatabaseTableName.ONBOARDING_ANSWERS_TO_USERS}.answerId`, | ||
to: `${DatabaseTableName.ONBOARDING_ANSWERS_TO_USERS}.userId`, | ||
}, | ||
to: `${DatabaseTableName.USERS}.id`, | ||
}, | ||
modelClass: UserModel, | ||
relation: Model.ManyToManyRelation, | ||
}, | ||
}; | ||
} | ||
|
||
public static override get tableName(): string { | ||
return DatabaseTableName.ONBOARDING_ANSWERS; | ||
} | ||
} | ||
|
||
export { OnboardingAnswerModel }; |
33 changes: 33 additions & 0 deletions
33
apps/backend/src/modules/onboarding/onboarding-question.model.ts
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,33 @@ | ||
import { Model, type RelationMappings } from "objection"; | ||
|
||
import { | ||
AbstractModel, | ||
DatabaseTableName, | ||
} from "~/libs/modules/database/database.js"; | ||
|
||
import { OnboardingAnswerModel } from "./onboarding-answer.model.js"; | ||
|
||
class OnboardingQuestionModel extends AbstractModel { | ||
public answers!: OnboardingAnswerModel[]; | ||
|
||
public label!: string; | ||
|
||
static get relationMappings(): RelationMappings { | ||
return { | ||
answers: { | ||
join: { | ||
from: `${DatabaseTableName.ONBOARDING_QUESTIONS}.id`, | ||
to: `${DatabaseTableName.ONBOARDING_ANSWERS}.questionId`, | ||
}, | ||
modelClass: OnboardingAnswerModel, | ||
relation: Model.HasManyRelation, | ||
}, | ||
}; | ||
} | ||
|
||
public static override get tableName(): string { | ||
return DatabaseTableName.ONBOARDING_QUESTIONS; | ||
} | ||
} | ||
|
||
export { OnboardingQuestionModel }; |
Oops, something went wrong.