Skip to content

Commit

Permalink
🔧 fix: add department test
Browse files Browse the repository at this point in the history
  • Loading branch information
Hendry Tanaka committed Jul 27, 2024
1 parent a9fccc6 commit 8ec0d78
Show file tree
Hide file tree
Showing 6 changed files with 402 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ export const mockMasterItemModel = {
update: jest.fn().mockResolvedValue(mockMasterItem()),
create: jest.fn().mockResolvedValue(mockMasterItem()),
save: jest.fn().mockImplementation(),
bulkSave: jest.fn().mockResolvedValue({}),
exec: jest.fn().mockImplementation(),
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,18 +169,20 @@ export class MasterDepartmentService {
{ upsert: false, new: true }
)
.then((result) => {
if (!result) {
response.statusCode = {
...modCodes[this.constructor.name].error.isNotFound,
classCode: modCodes[this.constructor.name].defaultCode,
}
response.message = 'Department failed to update'
response.payload = {}
throw new Error(JSON.stringify(response))
} else {
response.message = 'Department updated successfully'
response.payload = result
}
response.statusCode.customCode = !result
? modCodes[this.constructor.name].error.isNotFound.customCode
: response.statusCode.customCode

response.statusCode.defaultCode = !result
? modCodes[this.constructor.name].error.isNotFound.defaultCode
: response.statusCode.defaultCode

response.message = !result
? 'Department failed to update'
: 'Department updated successfully'

response.payload = result

return response
})
} catch (error) {
Expand Down
129 changes: 48 additions & 81 deletions server/apps/core/src/modules/master/services/master.item.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,47 +116,54 @@ export class MasterItemService {
}

async bulk(bulkData = []) {
if (bulkData.length > 0) {
const prepareBulk = await Promise.all(
bulkData.map(async (data) => {
let prepareData = await this.find({
$and: [{ code: data.code }],
}).then((r) => r)['payload']
if (!prepareData) {
prepareData = new this.masterItemModel({
code: data.code,
name: data.name,
alias: data.alias,
unit: data.unit,
category: data.category,
configuration: data.configuration,
brand: data.brand,
storing: data.storing,
remark: data.remark,
properties: data.properties,
})
} else {
prepareData.name = data.name
prepareData.alias = data.alias
prepareData.unit = data.unit
prepareData.category = data.category
// prepareData.configuration = data.configuration
prepareData.brand = data.brand
// prepareData.storing = data.storing
prepareData.remark = data.remark
prepareData.properties = data.properties
}
return prepareData
})
)
await this.masterItemModel
.bulkSave(prepareBulk, { ordered: false })
.then(() => {
//
})
.catch((e: Error) => {
throw e
})
const response = {
statusCode: {
defaultCode: HttpStatus.OK,
customCode: modCodes.Global.success,
classCode: modCodes[this.constructor.name].defaultCode,
},
message: '',
payload: {},
transaction_classify: 'MASTER_ITEM_IMPORT',
transaction_id: '',
} satisfies GlobalResponse

try {
if (bulkData.length > 0) {
const prepareBulk = await Promise.all(
bulkData.map(async (data) => {
let prepareData = await this.find({
$and: [{ code: data.code }],
}).then((r) => r)['payload']
prepareData = !prepareData ? { code: '' } : prepareData
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { code, ...dataRes } = prepareData
return !prepareData ? data : dataRes
})
)
return await this.masterItemModel
.bulkSave(prepareBulk, { ordered: false })
.then(() => {
response.message = 'Data imported successfully'
return response
})
} else {
response.message = `Data to import is empty`
response.statusCode = {
...modCodes[this.constructor.name].error.databaseError,
classCode: modCodes[this.constructor.name].defaultCode,
}
response.payload = bulkData
throw new Error(JSON.stringify(response))
}
} catch (error) {
response.message = error.message
response.statusCode = {
...modCodes[this.constructor.name].error.databaseError,
classCode: modCodes[this.constructor.name].defaultCode,
}
response.payload = error.stack
throw new Error(JSON.stringify(response))
}
}

Expand Down Expand Up @@ -285,44 +292,4 @@ export class MasterItemService {
throw new Error(JSON.stringify(response))
}
}

async import(file: string, account: Account): Promise<GlobalResponse> {
const response = {
statusCode: {
defaultCode: HttpStatus.OK,
customCode: modCodes.Global.success,
classCode: modCodes[this.constructor.name].defaultCode,
},
message: '',
payload: {},
transaction_classify: 'MASTER_ITEM_IMPORT',
transaction_id: null,
} satisfies GlobalResponse

// const emitter = await this.mItemClient.send({
// topic: 'master_item',
// messages: [
// {
// headers: {},
// key: {},
// value: {
// file: file,
// account: account,
// },
// },
// ],
// })
const emitter = true
if (emitter) {
response.message = 'Master item imported successfully'
} else {
response.message = `Master item failed to import`
response.statusCode = {
...modCodes[this.constructor.name].error.databaseError,
classCode: modCodes[this.constructor.name].defaultCode,
}
throw new Error(JSON.stringify(response))
}
return response
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
import {
MasterDepartmentAddDTO,
MasterDepartmentEditDTO,
} from '@core/master/dto/master.department'
import { faker } from '@faker-js/faker'
import { testCaption } from '@utility/string'
import { plainToInstance } from 'class-transformer'
import { validate } from 'class-validator'

const falseCasePayload = {
add: [
{
expectedToContain: 'code must be longer than or equal to 8 characters',
targetClass: MasterDepartmentAddDTO,
testType: -1,
data: {
code: faker.string.alpha({ length: 5, casing: 'upper' }),
name: faker.company.name(),
configuration: {
default_consultation_treatment: {},
treatment: [],
doctor: [],
},
},
},
{
expectedToContain: 'code must be shorter than or equal to 24 characters',
targetClass: MasterDepartmentAddDTO,
testType: -1,
data: {
code: faker.string.alpha({ length: 25, casing: 'upper' }),
name: faker.company.name(),
configuration: {
default_consultation_treatment: {},
treatment: [],
doctor: [],
},
},
},
{
expectedToContain: 'name should not be empty',
targetClass: MasterDepartmentAddDTO,
testType: -1,
data: {
code: faker.string.alpha({ length: 24, casing: 'upper' }),
configuration: {
default_consultation_treatment: {},
treatment: [],
doctor: [],
},
},
},
{
expectedToContain: 'Correct data',
targetClass: MasterDepartmentAddDTO,
testType: 1,
data: {
code: faker.string.alpha({ length: 24, casing: 'upper' }),
name: faker.company.name(),
configuration: {
default_consultation_treatment: {},
treatment: [],
doctor: [],
},
},
},
],
edit: [
{
expectedToContain: 'code must be longer than or equal to 8 characters',
targetClass: MasterDepartmentEditDTO,
testType: -1,
data: {
code: faker.string.alpha({ length: 5, casing: 'upper' }),
name: faker.company.name(),
configuration: {
default_consultation_treatment: {},
treatment: [],
doctor: [],
},
__v: 0,
},
},
{
expectedToContain: 'code must be shorter than or equal to 24 characters',
targetClass: MasterDepartmentEditDTO,
testType: -1,
data: {
code: faker.string.alpha({ length: 25, casing: 'upper' }),
name: faker.company.name(),
configuration: {
default_consultation_treatment: {},
treatment: [],
doctor: [],
},
__v: 0,
},
},
{
expectedToContain: 'name should not be empty',
targetClass: MasterDepartmentEditDTO,
testType: -1,
data: {
code: faker.string.alpha({ length: 24, casing: 'upper' }),
configuration: {
default_consultation_treatment: {},
treatment: [],
doctor: [],
},
__v: 0,
},
},
{
expectedToContain: '__v should not be empty',
targetClass: MasterDepartmentEditDTO,
testType: -1,
data: {
code: faker.string.alpha({ length: 24, casing: 'upper' }),
name: faker.company.name(),
configuration: {
default_consultation_treatment: {},
treatment: [],
doctor: [],
},
},
},
{
expectedToContain: 'Correct data',
targetClass: MasterDepartmentEditDTO,
testType: 1,
data: {
code: faker.string.alpha({ length: 24, casing: 'upper' }),
name: faker.company.name(),
configuration: {
default_consultation_treatment: {},
treatment: [],
doctor: [],
},
__v: 0,
},
},
],
}
describe('Master Department DTO Test', () => {
describe(testCaption('ADD', 'data', 'Master department add'), () => {
for (const tKey of falseCasePayload.add) {
it(
testCaption(
tKey.testType < 0 ? 'NEGATIVE' : 'POSITIVE',
'data',
`${tKey.expectedToContain}`,
{
tab: 1,
}
),
async () => {
const ofImportDto = plainToInstance(tKey.targetClass, tKey.data)
const errors = await validate(ofImportDto)
if (tKey.testType < 0) {
expect(errors.length).not.toBe(0)
expect(JSON.stringify(errors)).toContain(tKey.expectedToContain)
} else {
expect(errors.length).toBe(0)
}
}
)
}
})

describe(testCaption('EDIT', 'data', 'Master department edit'), () => {
for (const tKey of falseCasePayload.edit) {
it(
testCaption(
tKey.testType < 0 ? 'NEGATIVE' : 'POSITIVE',
'data',
`${tKey.expectedToContain}`,
{
tab: 1,
}
),
async () => {
const ofImportDto = plainToInstance(tKey.targetClass, tKey.data)
const errors = await validate(ofImportDto)
if (tKey.testType < 0) {
expect(errors.length).not.toBe(0)
expect(JSON.stringify(errors)).toContain(tKey.expectedToContain)
} else {
expect(errors.length).toBe(0)
}
}
)
}
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ describe('Master Department Service', () => {
async () => {
jest
.spyOn(masterDepartmentModel, 'findOneAndUpdate')
.mockReturnValue(undefined)
.mockReturnValue(null)

await expect(async () => {
await masterDepartmentService.edit(
Expand Down
Loading

0 comments on commit 8ec0d78

Please sign in to comment.