Skip to content

Commit 8361897

Browse files
committed
x
1 parent 3501d2a commit 8361897

File tree

8 files changed

+127
-0
lines changed

8 files changed

+127
-0
lines changed

src/modules/users/controllers/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export { retrieveAllUserController } from './retrieve-all.controller'
1010
export { signinController } from './signin.controller'
1111
export { signupController } from './signup.controller'
1212
export { updateUserController } from './update.controller'
13+
export { updatePasswordUserController } from './update-password.controller'
1314
// export { updateManyUserController } from './update-many.controller'
1415
export { verifyEmailController } from './verify-email.controller'
1516
export { verifyTokenController } from './verify-token.controller'
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { objClean } from '@point-hub/express-utils'
2+
import type { IController, IControllerInput } from '@point-hub/papi'
3+
4+
import { UniqueValidation } from '@/utils/unique-validation'
5+
import { schemaValidation } from '@/utils/validation'
6+
7+
import { UpdatePasswordUserRepository } from '../repositories/update-password.repository'
8+
import { UpdatePasswordUserUseCase } from '../use-cases/update-password.use-case'
9+
10+
export const updatePasswordUserController: IController = async (controllerInput: IControllerInput) => {
11+
let session
12+
try {
13+
// 1. start session for transactional
14+
session = controllerInput.dbConnection.startSession()
15+
session.startTransaction()
16+
// 2. define repository
17+
const updatePasswordUserRepository = new UpdatePasswordUserRepository(controllerInput.dbConnection, { session })
18+
const uniqueValidation = new UniqueValidation(controllerInput.dbConnection, { session })
19+
// 3. handle business rules
20+
const response = await UpdatePasswordUserUseCase.handle(
21+
{
22+
_id: controllerInput.httpRequest['params'].id,
23+
data: controllerInput.httpRequest['body'],
24+
},
25+
{ schemaValidation, updatePasswordUserRepository, uniqueValidation, objClean, hashPassword: Bun.password.hash },
26+
)
27+
await session.commitTransaction()
28+
// 4. return response to client
29+
return {
30+
status: 200,
31+
json: {
32+
matched_count: response.matched_count,
33+
modified_count: response.modified_count,
34+
},
35+
}
36+
} catch (error) {
37+
await session?.abortTransaction()
38+
throw error
39+
} finally {
40+
await session?.endSession()
41+
}
42+
}

src/modules/users/repositories/index.repository.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ export { RetrieveAllUserRepository } from './retrieve-all.repository'
77
export { SignupRepository } from './signup.repository'
88
export { UpdateUserRepository } from './update.repository'
99
export { UpdateManyUserRepository } from './update-many.repository'
10+
export { UpdatePasswordUserRepository } from './update-password.repository'
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import type { IDatabase, IDocument } from '@point-hub/papi'
2+
3+
import { collectionName } from '../entity'
4+
5+
export interface IUpdatePasswordUserRepository {
6+
handle(_id: string, document: IDocument): Promise<IUpdatePasswordUserOutput>
7+
}
8+
9+
export interface IUpdatePasswordUserOutput {
10+
matched_count: number
11+
modified_count: number
12+
}
13+
14+
export class UpdatePasswordUserRepository implements IUpdatePasswordUserRepository {
15+
constructor(
16+
public database: IDatabase,
17+
public options?: Record<string, unknown>,
18+
) {}
19+
20+
async handle(_id: string, document: IDocument): Promise<IUpdatePasswordUserOutput> {
21+
console.log(document)
22+
return await this.database
23+
.collection(collectionName)
24+
.update(_id, { $set: document }, { ignoreUndefined: true, ...this.options })
25+
}
26+
}

src/modules/users/router.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ const makeRouter = async (routerInput: IBaseAppInput) => {
3636
dbConnection: routerInput.dbConnection,
3737
}),
3838
)
39+
router.post(
40+
'/:id/update-password',
41+
await makeController({
42+
controller: controller.updatePasswordUserController,
43+
dbConnection: routerInput.dbConnection,
44+
}),
45+
)
3946
router.delete(
4047
'/:id',
4148
await makeController({
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import type { IObjClean } from '@point-hub/express-utils'
2+
import type { ISchemaValidation } from '@point-hub/papi'
3+
4+
import type { IUniqueValidation } from '@/utils/unique-validation'
5+
6+
import type { IUpdatePasswordUserRepository } from '../repositories/update-password.repository'
7+
import { updatePasswordValidation } from '../validations/update-password.validation'
8+
9+
export interface IInput {
10+
_id: string
11+
data: {
12+
password?: string
13+
}
14+
}
15+
16+
export interface IDeps {
17+
schemaValidation: ISchemaValidation
18+
updatePasswordUserRepository: IUpdatePasswordUserRepository
19+
uniqueValidation: IUniqueValidation
20+
hashPassword(password: string): Promise<string>
21+
objClean: IObjClean
22+
}
23+
24+
export interface IOutput {
25+
matched_count: number
26+
modified_count: number
27+
}
28+
29+
export class UpdatePasswordUserUseCase {
30+
static async handle(input: IInput, deps: IDeps): Promise<IOutput> {
31+
// 2. validate schema
32+
33+
await deps.schemaValidation(input.data, updatePasswordValidation)
34+
// 3. define entity
35+
36+
// 4. database operation
37+
const response = await deps.updatePasswordUserRepository.handle(input._id, {
38+
password: await deps.hashPassword(input.data.password as string),
39+
})
40+
// 5. output
41+
return {
42+
matched_count: response.matched_count,
43+
modified_count: response.modified_count,
44+
}
45+
}
46+
}

src/modules/users/use-cases/update.use-case.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export interface IOutput {
3232
export class UpdateUserUseCase {
3333
static async handle(input: IInput, deps: IDeps): Promise<IOutput> {
3434
// 1. validate unique
35+
await deps.uniqueValidation.handle(collectionName, { username: input.data.username }, input._id)
3536
await deps.uniqueValidation.handle(collectionName, { name: input.data.name }, input._id)
3637
// 2. validate schema
3738
await deps.schemaValidation(input.data, updateValidation)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export const updatePasswordValidation = {
2+
password: ['required', 'string'],
3+
}

0 commit comments

Comments
 (0)