-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ad61f08
commit 51f12da
Showing
10 changed files
with
10,470 additions
and
3 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
9 changes: 9 additions & 0 deletions
9
backend/src/modules/questions/core/application/GroupQuestionsRepository.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,9 @@ | ||
import { GroupQuestions } from '../domain/GroupQuestions'; | ||
|
||
export interface GroupQuestionsRepository { | ||
save(groupQuestions: GroupQuestions): Promise<void>; | ||
|
||
findByGroupId(groupId: string): Promise<GroupQuestions | undefined>; | ||
|
||
findAll(): Promise<GroupQuestions[]>; | ||
} |
30 changes: 30 additions & 0 deletions
30
backend/src/modules/questions/core/application/event/GroupQuestionAskedEventHandler.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,30 @@ | ||
import { EventHandler } from '../../../../../shared/core/application/event/EventHandler'; | ||
import { GroupQuestionsRepository } from '../GroupQuestionsRepository'; | ||
import { GroupQuestions } from '../../domain/GroupQuestions'; | ||
import { GroupQuestionWasAsked } from '../../../../asking-question/core/domain/event/GroupQuestionWasAsked'; | ||
|
||
export class GroupQuestionAskedEventHandler implements EventHandler<GroupQuestionWasAsked> { | ||
constructor(private readonly groupQuestionsRepository: GroupQuestionsRepository) {} | ||
|
||
async handle(event: GroupQuestionWasAsked): Promise<void> { | ||
const questionId = event.questionId; | ||
const groupId = event.groupId; | ||
|
||
const groupQuestions = await this.groupQuestionsRepository.findByGroupId(groupId); | ||
|
||
const existingQuestions = groupQuestions!.questionList.filter((elem) => elem.questionId !== questionId); | ||
|
||
const newGroupQuestions = new GroupQuestions({ | ||
questionList: existingQuestions, | ||
questionAskedLastlyDate: new Date(), | ||
groupId: groupQuestions!.groupId, | ||
questionAskedLastly: { | ||
questionId: questionId, | ||
text: event.text, | ||
authorId: event.askedBy, | ||
groupId: groupId, | ||
}, | ||
}); | ||
await this.groupQuestionsRepository.save(newGroupQuestions); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
backend/src/modules/questions/core/application/event/QuestionWasDefinedEventHandler.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,38 @@ | ||
import { EventHandler } from '../../../../../shared/core/application/event/EventHandler'; | ||
import { GroupQuestionsRepository } from '../GroupQuestionsRepository'; | ||
import { Question } from '../../domain/Question'; | ||
import { GroupQuestions } from '../../domain/GroupQuestions'; | ||
import { QuestionWasDefined } from '../../domain/event/QuestionWasDefined'; | ||
|
||
export class QuestionWasDefinedEventHandler implements EventHandler<QuestionWasDefined> { | ||
constructor(private readonly groupQuestionsRepository: GroupQuestionsRepository) {} | ||
|
||
async handle(event: QuestionWasDefined): Promise<void> { | ||
const questionId = event.questionId; | ||
const groupId = event.groupId; | ||
const question = { | ||
questionId: questionId, | ||
text: event.text, | ||
authorId: event.authorId, | ||
groupId: groupId, | ||
}; | ||
const groupQuestions = await this.groupQuestionsRepository.findByGroupId(groupId); | ||
|
||
if (!groupQuestions) { | ||
const questionList: Question[] = [question]; | ||
const groupQuestions = new GroupQuestions({ | ||
groupId: groupId, | ||
questionAskedLastly: undefined, | ||
questionAskedLastlyDate: new Date(), | ||
questionList: questionList, | ||
}); | ||
await this.groupQuestionsRepository.save(groupQuestions); | ||
} else { | ||
const existingQuestions = groupQuestions.questionList.filter((elem) => elem.authorId !== event.authorId); | ||
|
||
existingQuestions.push(question); | ||
const newGroupQuestions = new GroupQuestions({ ...groupQuestions, questionList: existingQuestions }); | ||
await this.groupQuestionsRepository.save(newGroupQuestions); | ||
} | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
backend/src/modules/questions/core/application/event/TimeHasPassedEventHandler.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,39 @@ | ||
import { EventHandler } from '../../../../../shared/core/application/event/EventHandler'; | ||
import { TimeHasPassed } from '../../../../time/core/domain/event/TimeHasPassed'; | ||
import { GroupQuestionsRepository } from '../GroupQuestionsRepository'; | ||
import { GroupQuestions } from '../../domain/GroupQuestions'; | ||
import { CommandPublisher } from '../../../../../shared/core/application/command/CommandBus'; | ||
import { AskGroupQuestion } from '../../../../asking-question/core/application/command/AskGroupQuestion'; | ||
|
||
export class TimeHasPassedEventHandler implements EventHandler<TimeHasPassed> { | ||
constructor(private readonly groupQuestionsRepository: GroupQuestionsRepository, private readonly commandPublisher: CommandPublisher) {} | ||
|
||
async handle(event: TimeHasPassed): Promise<void> { | ||
const groupQuestions = await this.groupQuestionsRepository.findAll(); | ||
if (groupQuestions.length === 0) { | ||
return; | ||
} | ||
const actualDate = event.occurredAt; | ||
const actualDay = actualDate.getDay(); | ||
if (actualDay === 0 || actualDay === 6) { | ||
return; | ||
} | ||
if (actualDay !== groupQuestions[0].questionAskedLastlyDate!.getDay()) { | ||
getQuestionsToAsk(groupQuestions, this.commandPublisher); | ||
} | ||
} | ||
} | ||
|
||
function getQuestionsToAsk(groupsQuestions: GroupQuestions[], commandPublisher: CommandPublisher) { | ||
groupsQuestions.forEach(async (groupQuestions) => { | ||
const randomQuestion = groupQuestions.questionList[Math.floor(Math.random() * groupQuestions.questionList.length)]; | ||
await commandPublisher.execute( | ||
new AskGroupQuestion({ | ||
questionId: randomQuestion.questionId, | ||
groupId: randomQuestion.groupId, | ||
authorId: randomQuestion.authorId, | ||
text: randomQuestion.text, | ||
}), | ||
); | ||
}); | ||
} |
20 changes: 20 additions & 0 deletions
20
backend/src/modules/questions/core/domain/GroupQuestions.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,20 @@ | ||
import { Question } from './Question'; | ||
|
||
export class GroupQuestions { | ||
readonly groupId: string; | ||
readonly questionAskedLastly: Question | undefined; | ||
readonly questionAskedLastlyDate: Date; | ||
readonly questionList: Question[]; | ||
|
||
constructor(props: { | ||
groupId: string; | ||
questionAskedLastly: Question | undefined; | ||
questionAskedLastlyDate: Date; | ||
questionList: Question[]; | ||
}) { | ||
this.groupId = props.groupId; | ||
this.questionAskedLastly = props.questionAskedLastly; | ||
this.questionAskedLastlyDate = props.questionAskedLastlyDate; | ||
this.questionList = props.questionList; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
.../modules/questions/infrastructure/repository/inmemory/InMemoryGroupQuestionsRepository.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,18 @@ | ||
import { GroupQuestionsRepository } from '../../../core/application/GroupQuestionsRepository'; | ||
import { GroupQuestions } from '../../../core/domain/GroupQuestions'; | ||
|
||
export class InMemoryGroupQuestionsRepository implements GroupQuestionsRepository { | ||
private readonly entities: { [groupId: string]: GroupQuestions } = {}; | ||
|
||
findByGroupId(groupId: string): Promise<GroupQuestions | undefined> { | ||
return Promise.resolve(this.entities[groupId]); | ||
} | ||
|
||
async save(groupQuestions: GroupQuestions): Promise<void> { | ||
this.entities[groupQuestions.groupId] = groupQuestions; | ||
} | ||
|
||
findAll(): Promise<GroupQuestions[]> { | ||
return Promise.resolve(Object.keys(this.entities).map((groupId) => this.entities[groupId])); | ||
} | ||
} |
6 changes: 5 additions & 1 deletion
6
backend/test/modules/questions/core/application/TestQuestionsModule.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 |
---|---|---|
@@ -1,8 +1,12 @@ | ||
import { InMemoryQuestionsRepository } from '../../../../../src/modules/questions/infrastructure/repository/inmemory/InMemoryQuestionsRepository'; | ||
import { TestModuleCore, testModuleCore } from '../../../../test-support/shared/core/TestModuleCore'; | ||
import { QuestionsModuleCore } from '../../../../../src/modules/questions/core/QuestionsModuleCore'; | ||
import { InMemoryGroupQuestionsRepository } from '../../../../../src/modules/questions/infrastructure/repository/inmemory/InMemoryGroupQuestionsRepository'; | ||
|
||
export function testQuestionsModule(currentTime: Date): TestModuleCore { | ||
const questionsRepository = new InMemoryQuestionsRepository(); | ||
return testModuleCore((commandBus, eventBus, queryBus) => QuestionsModuleCore(eventBus, () => currentTime, questionsRepository)); | ||
const groupQuestionsRepository = new InMemoryGroupQuestionsRepository(); | ||
return testModuleCore((commandBus, eventBus, queryBus) => | ||
QuestionsModuleCore(eventBus, commandBus, () => currentTime, groupQuestionsRepository, questionsRepository), | ||
); | ||
} |