Skip to content

Commit

Permalink
refactor: conversations requests to tutors
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinand11 committed Dec 10, 2023
1 parent 3856f12 commit deac4cd
Show file tree
Hide file tree
Showing 27 changed files with 151 additions and 518 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,27 @@ export class ConversationController {
}

static async create (req: Request) {
const { body } = validate({
const { body, tutorId } = validate({
body: Schema.string().min(1),
tutorId: Schema.string().min(1).nullable(),
}, req.body)

const authUserId = req.authUser!.id
const user = await UsersUseCases.find(authUserId)
if (!user || user.isDeleted()) throw new BadRequestError('user not found')

const title = await AI.summarizeForTitle(body)
const conversation = await ConversationsUseCases.add({ title, user: user.getEmbedded() })
const tutor = tutorId ? await UsersUseCases.find(tutorId) : null
if (tutorId) {
if (!tutor || tutor.isDeleted()) throw new BadRequestError('tutor not found')
if (!tutor.canJoinConversations()) throw new BadRequestError('tutor can\'t join conversations right now')
}

const title = tutorId ? body : await AI.summarizeForTitle(body)
const conversation = await ConversationsUseCases.add({
title, user: user.getEmbedded(),
pending: !!tutorId, accepted: !tutorId ? { is: true, at: Date.now() } : null,
tutor: tutor?.getEmbedded() ?? null
})
await MessagesUseCases.add({
body, media: null, starred: false,
conversationId: conversation.id,
Expand Down Expand Up @@ -59,18 +70,22 @@ export class ConversationController {
throw new NotAuthorizedError()
}

static async removeTutor (req: Request) {
static async accept (req: Request) {
const { accept } = validate({ accept: Schema.boolean() }, req.body)
const isUpdated = await ConversationsUseCases.accept({ id: req.params.id, accept, tutorId: req.authUser!.id })
if (isUpdated) return isUpdated
throw new NotAuthorizedError()
}

static async end (req: Request) {
const { rating, message } = validate({
rating: Schema.number().round(0).gte(0).lte(5),
message: Schema.string()
}, req.body)

const user = await UsersUseCases.find(req.authUser!.id)
if (!user || user.isDeleted()) throw new BadRequestError('profile not found')

const updatedConversation = await ConversationsUseCases.removeTutor({
const updatedConversation = await ConversationsUseCases.end({
rating, message, conversationId: req.params.id,
user: user.getEmbedded()
userId: req.authUser!.id
})

if (updatedConversation) return updatedConversation
Expand Down

This file was deleted.

18 changes: 15 additions & 3 deletions services/api/src/application/routes/conversations/conversations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,26 @@ export const conversationsRoutes = groupRoutes('/conversations', [
})
]
}, {
path: '/:id/tutor',
method: 'delete',
path: '/:id/accept',
method: 'post',
controllers: [
isAuthenticated,
makeController(async (req) => {
return {
status: StatusCodes.Ok,
result: await ConversationController.accept(req)
}
})
]
}, {
path: '/:id/end',
method: 'post',
controllers: [
isAuthenticated,
makeController(async (req) => {
return {
status: StatusCodes.Ok,
result: await ConversationController.removeTutor(req)
result: await ConversationController.end(req)
}
})
]
Expand Down
2 changes: 0 additions & 2 deletions services/api/src/application/routes/conversations/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { groupRoutes } from 'equipped'
import { conversationsRoutes } from './conversations'
import { messagesRoutes } from './messages'
import { tutorRequestRoutes } from './tutorRequests'

export const conversationRoutes = groupRoutes('/conversations', [
...conversationsRoutes,
...messagesRoutes,
...tutorRequestRoutes
])
67 changes: 0 additions & 67 deletions services/api/src/application/routes/conversations/tutorRequests.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,21 @@ import { MessageMapper } from './messages'
export class ConversationMapper extends BaseMapper<ConversationFromModel, ConversationToModel, ConversationEntity> {
mapFrom (model: ConversationFromModel | null) {
if (!model) return null
const { _id, title, user, tutor, createdAt, updatedAt, last, readAt } = model
const { _id, title, user, tutor, pending, accepted, ended, createdAt, updatedAt, last, readAt } = model
const lastData = new MessageMapper().mapFrom(last)
return new ConversationEntity({
id: _id.toString(), title, user, tutor, createdAt, updatedAt,
last: lastData, readAt
pending, accepted, ended, last: lastData, readAt
})
}

mapTo (entity: ConversationEntity) {
return {
title: entity.title,
user: entity.user
user: entity.user,
tutor: entity.tutor,
pending: entity.pending,
accepted: entity.accepted
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { MessageFromModel } from './messages'

export interface ConversationFromModel extends ConversationToModel {
_id: string
tutor: EmbeddedUser | null
ended: { rating: number, message: string, at: number } | null
createdAt: number
updatedAt: number
readAt: Record<string, number>
Expand All @@ -13,4 +13,7 @@ export interface ConversationFromModel extends ConversationToModel {
export interface ConversationToModel {
title: string
user: EmbeddedUser
tutor: EmbeddedUser | null
pending: boolean
accepted: { is: boolean, at: number } | null
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,21 @@ const Schema = new appInstance.dbs.mongo.Schema<ConversationFromModel>({
required: false,
default: null
},
pending: {
type: Boolean,
required: false,
default: true
},
accepted: {
type: appInstance.dbs.mongo.Schema.Types.Mixed,
required: false,
default: null
},
ended: {
type: appInstance.dbs.mongo.Schema.Types.Mixed,
required: false,
default: null
},
createdAt: {
type: Number,
required: false,
Expand Down

This file was deleted.

Loading

0 comments on commit deac4cd

Please sign in to comment.