-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
mongoose model update version file added
- Loading branch information
1 parent
4a95bd9
commit 43ad90f
Showing
9 changed files
with
565 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
import { Model, Document } from 'mongoose'; | ||
import { AuditModel } from '../models/audit.model'; | ||
import { FilterModel } from '../models/filter.model'; | ||
|
||
interface A extends AuditModel, Document {} | ||
export class ResourceService<T extends A, D extends any> { | ||
constructor(protected readonly mongoModel: Model<T>) {} | ||
|
||
generalSearchQuery = { | ||
page: 1, | ||
size: 10, | ||
sort: 'asc', | ||
sortBy: '_id', | ||
queryText: '', | ||
searchBy: 'name', | ||
}; | ||
|
||
async create(model: D): Promise<T> { | ||
const audit = new AuditModel(); | ||
audit.active = true; | ||
audit.createdBy = 'Admin'; | ||
audit.createdDate = new Date(); | ||
const newModal = { ...audit, ...(model as {}) }; | ||
|
||
const createdNew = new this.mongoModel(newModal); | ||
return await createdNew.save(); | ||
} | ||
|
||
async findAll(query?: FilterModel): Promise<any[]> { | ||
if (query && Object.keys(query).length !== 0) { | ||
const searchValue = await { ...this.generalSearchQuery, ...query }; | ||
const filterRegex = new RegExp(searchValue.queryText, 'i'); | ||
const today = new Date(); | ||
// previous 2 weeks | ||
today.setHours(today.getHours() - 24 * 14); | ||
|
||
const count = await this.mongoModel.countDocuments({}).exec(); | ||
let data; | ||
if (query?.startDate && query?.endDate) { | ||
data = await this.mongoModel | ||
.find({ | ||
[searchValue.searchBy]: filterRegex, | ||
'audit.createdDate': { | ||
$gte: new Date( | ||
query.startDate ? query.startDate : today.toISOString(), | ||
), | ||
$lt: new Date( | ||
query.endDate ? query.endDate : new Date().toISOString(), | ||
), | ||
}, | ||
} as any) | ||
.limit(Math.max(0, searchValue.size)) | ||
.skip(searchValue.size * (searchValue.page - 1)) | ||
.sort([[`${searchValue.sortBy}`, searchValue.sort]]) | ||
.exec(); | ||
} else { | ||
data = await this.mongoModel | ||
.find({ | ||
[searchValue.searchBy]: filterRegex, | ||
} as any) | ||
.limit(Math.max(0, searchValue.size)) | ||
.skip(searchValue.size * (searchValue.page - 1)) | ||
.sort([[`${searchValue.sortBy}`, searchValue.sort]]) | ||
.exec(); | ||
} | ||
|
||
return await [ | ||
{ | ||
success: true, | ||
size: query.size ? query.size : this.generalSearchQuery.size, | ||
total: count, | ||
data, | ||
}, | ||
]; | ||
} else { | ||
const count = await this.mongoModel.countDocuments({}).exec(); | ||
const data = await this.mongoModel.find().exec(); | ||
return await [ | ||
{ | ||
success: true, | ||
size: this.generalSearchQuery.size, | ||
total: count, | ||
data, | ||
}, | ||
]; | ||
} | ||
} | ||
|
||
async findOne(id: string): Promise<T> { | ||
try { | ||
return await this.mongoModel.findOne({ _id: id as any }).exec(); | ||
} catch (error) { | ||
return await undefined; | ||
} | ||
} | ||
|
||
async findOneByName(name: string): Promise<T> { | ||
return await this.mongoModel.findOne({ name: name as any }).exec(); | ||
} | ||
|
||
async findOneByEmail(email: string): Promise<T> { | ||
return await this.mongoModel.findOne({ email: email as any }).exec(); | ||
} | ||
|
||
async findOneBySerialId(serial_id: string): Promise<T> { | ||
return await this.mongoModel | ||
.findOne({ serial_id: serial_id as any }) | ||
.sort([[`audit.createdDate`, 'DESC']]) | ||
.exec(); | ||
} | ||
|
||
async delete(id: string): Promise<T> { | ||
return await this.mongoModel.findByIdAndRemove({ _id: id }).exec(); | ||
} | ||
|
||
async update(id: string, dto: D): Promise<T> { | ||
let newModal = this.mongoModel.findOne({ _id: id as any }).exec(); | ||
newModal = { ...newModal, ...(dto as {}) }; | ||
return await this.mongoModel | ||
.findByIdAndUpdate(id, newModal as any, { new: true }) | ||
.exec(); | ||
} | ||
} |
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 { Test, TestingModule } from '@nestjs/testing'; | ||
import { UserController } from './user.controller'; | ||
|
||
describe('UserController', () => { | ||
let controller: UserController; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
controllers: [UserController], | ||
}).compile(); | ||
|
||
controller = module.get<UserController>(UserController); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(controller).toBeDefined(); | ||
}); | ||
}); |
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,254 @@ | ||
import { | ||
Controller, | ||
Get, | ||
Param, | ||
Post, | ||
Body, | ||
Put, | ||
Delete, | ||
Query, | ||
} from '@nestjs/common'; | ||
import { UserService } from './user.service'; | ||
import { UserModel } from './user.model'; | ||
import { | ||
UserCreateDto, | ||
UserForgotPasswordDto, | ||
UserPasswordResetDto, | ||
UserUpdateDto, | ||
} from './user.dto'; | ||
import { LoginService } from 'src/login/login.service'; | ||
import { FilterModel } from 'libs/models/filter.model'; | ||
import { AuditModel } from 'libs/models/audit.model'; | ||
import { RoleService } from 'src/role/role.service'; | ||
import { EndpointPermissions } from 'libs/decorator/permission.decorator'; | ||
import { MailService } from 'libs/services/mail.service'; | ||
import { MailModel } from 'libs/models/mail.model'; | ||
import environment from 'src/environment'; | ||
|
||
@Controller('user') | ||
export class UserController { | ||
constructor( | ||
private readonly userService: UserService, | ||
private readonly loginService: LoginService, | ||
private readonly roleService: RoleService, | ||
private readonly mailService: MailService, | ||
) {} | ||
|
||
@Post() | ||
// @EndpointPermissions('post:user') | ||
async CreateUser(@Body() body: UserCreateDto): Promise<any> { | ||
body.password = await this.loginService.convertToHash(body.password); | ||
const doesExist = await this.userService.findOneByEmail(body.email); | ||
if (doesExist) { | ||
return await { | ||
response: 'User already exist!', | ||
user: doesExist, | ||
success: false, | ||
}; | ||
// throw new HttpException( | ||
// { | ||
// status: HttpStatus.NOT_ACCEPTABLE, | ||
// error: 'User has already exist!', | ||
// user: doesExist, | ||
// success: false, | ||
// }, | ||
// HttpStatus.NOT_ACCEPTABLE, | ||
// ); | ||
} else { | ||
let newAudit = new AuditModel(); | ||
newAudit.active = true; | ||
newAudit.createdBy = 'Admin'; | ||
newAudit.createdDate = new Date(); | ||
body.audit = newAudit; | ||
|
||
let roleFail = false; | ||
if (body.roles && body.roles.length > 0) { | ||
body.roles.forEach((element) => { | ||
if (!this.roleService.findOne(element['_id'])) { | ||
roleFail = true; | ||
} | ||
}); | ||
if (roleFail) { | ||
return await { | ||
response: 'Some of Roles does not exist!', | ||
roles: body.roles, | ||
success: false, | ||
}; | ||
} else { | ||
return this.userService.create(body); | ||
} | ||
} else { | ||
return this.userService.create(body); | ||
} | ||
} | ||
} | ||
|
||
@Post('/password-reset') | ||
async passwordReset( | ||
@Body() UserPasswordResetDto: UserPasswordResetDto, | ||
): Promise<any> { | ||
let doesExist; | ||
if (await UserPasswordResetDto._id) { | ||
doesExist = await this.userService.findOne(UserPasswordResetDto._id); | ||
} else if (await UserPasswordResetDto.email) { | ||
doesExist = await this.userService.findOneByEmail( | ||
UserPasswordResetDto.email, | ||
); | ||
} else { | ||
doesExist = false; | ||
} | ||
|
||
try { | ||
if (doesExist) { | ||
if (doesExist.securityCode === UserPasswordResetDto.securityCode) { | ||
let checkPwd; | ||
if (await UserPasswordResetDto.oldPassword) { | ||
//change pwd | ||
checkPwd = await this.loginService.compareHashes( | ||
UserPasswordResetDto.oldPassword, | ||
doesExist.password, | ||
); | ||
} else { | ||
//reset pwd | ||
checkPwd = true; | ||
} | ||
const newPwd = await this.loginService.convertToHash( | ||
UserPasswordResetDto.newPassword, | ||
); | ||
if (checkPwd) { | ||
const existUser = new UserUpdateDto(); | ||
existUser.password = newPwd; | ||
return this.userService.update(doesExist._id, existUser); | ||
} else { | ||
return await { response: 'Incorrect old password', success: false }; | ||
} | ||
} else { | ||
return await { response: 'Security Code incorrect!', success: false }; | ||
} | ||
} else { | ||
return await { | ||
response: 'User does not exist!', | ||
success: false, | ||
}; | ||
} | ||
} catch (error) { | ||
return await { | ||
response: 'Something went wrong trying to password reset!', | ||
success: false, | ||
}; | ||
} | ||
} | ||
|
||
@Post('/forgot-password') | ||
async forgotPassword( | ||
@Body() forgotPwdDto: UserForgotPasswordDto, | ||
): Promise<any> { | ||
const doesExist = await this.userService.findOneByEmail(forgotPwdDto.email); | ||
|
||
try { | ||
if (doesExist) { | ||
// create random 8 digit number | ||
const randomDigits = Math.floor(10000000 + Math.random() * 90000000); | ||
|
||
// send an email | ||
const mailTemp = new MailModel(); | ||
mailTemp.from = process.env.EMAIL_ADDRESS || environment.email_address; | ||
mailTemp.to = forgotPwdDto.email; | ||
mailTemp.subject = | ||
process.env.EMAIL_SUBJECT || environment.email_subject; | ||
mailTemp.text = `${randomDigits} created-text`; | ||
mailTemp.html = { | ||
name: doesExist.name, | ||
surname: doesExist.surname, | ||
secureCode: randomDigits, | ||
}; | ||
|
||
const mailResp = await this.mailService | ||
.send(mailTemp) | ||
.then(async (resp) => { | ||
return resp; | ||
}) | ||
.catch(async (err) => { | ||
return err; | ||
}); | ||
|
||
if ((await mailResp) && (await mailResp.messageId)) { | ||
const existUser = new UserUpdateDto(); | ||
existUser.securityCode = randomDigits; | ||
await this.userService.update(doesExist._id, existUser); | ||
return await { | ||
response: 'Password reset email sent!', | ||
success: true, | ||
}; | ||
} else { | ||
return await { | ||
response: 'Something went wrong while sending an email!', | ||
success: false, | ||
}; | ||
} | ||
} else { | ||
return await { response: 'User does not exist', success: false }; | ||
} | ||
} catch (error) { | ||
return await { | ||
response: 'Something went wrong during process!', | ||
success: false, | ||
}; | ||
} | ||
} | ||
|
||
@Get() | ||
@EndpointPermissions('get:user') | ||
async getAllUsers(@Query() query: FilterModel): Promise<UserModel[]> { | ||
return this.userService.findAll(query); | ||
} | ||
|
||
@Get(':id') | ||
// @EndpointPermissions('get:user:id') | ||
async GetUser(@Param() params): Promise<UserModel> { | ||
return this.userService.findOne(params.id); | ||
} | ||
|
||
@Put(':id') | ||
@EndpointPermissions('put:user') | ||
async updateUser( | ||
@Param('id') id: string, | ||
@Body() UserUpdateDto: UserUpdateDto, | ||
): Promise<any> { | ||
let roleFail = false; | ||
if (UserUpdateDto.roles && UserUpdateDto.roles.length > 0) { | ||
UserUpdateDto.roles.forEach((element) => { | ||
if (!this.roleService.findOne(element['_id'])) { | ||
roleFail = true; | ||
} | ||
delete element['permissions']; | ||
return element; | ||
}); | ||
if (roleFail) { | ||
return await { | ||
response: 'Some of Roles does not exist!', | ||
roles: UserUpdateDto.roles, | ||
success: false, | ||
}; | ||
} else { | ||
return this.userService.update(id, UserUpdateDto); | ||
} | ||
} else { | ||
return this.userService.update(id, UserUpdateDto); | ||
} | ||
} | ||
|
||
@Delete(':id') | ||
@EndpointPermissions('delete:user') | ||
async removeUser(@Param('id') id: string): Promise<any> { | ||
const res = await this.userService.delete(id); | ||
if (res) { | ||
return res; | ||
} else { | ||
return await { | ||
response: 'User does not exist!', | ||
success: false, | ||
}; | ||
} | ||
} | ||
} |
Oops, something went wrong.