Skip to content

Commit 6f32eda

Browse files
committed
refactoring: termin, pengajuan pks
1 parent ff5392f commit 6f32eda

File tree

9 files changed

+137
-110
lines changed

9 files changed

+137
-110
lines changed

src/data/data-sources/purchasing/purchasing-detail-data-source.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export class PurchasingDetailDataSource implements IPurchasingDetailDataSource {
5050
}
5151

5252
const [rows, fields] = await this.dql.dataQueryLanguage(
53-
`SELECT (p.quantity - SUM(pd.quantity_payment)) AS remain_quantity_termin
53+
`SELECT (p.quantity - SUM(pd.quantity_payment)) AS remain_quantity_termin, p.isTermin as isTermin
5454
FROM purchasing AS p
5555
INNER JOIN purchasing_detail AS pd
5656
ON pd.purchasing_id = p.purchasing_id ${whereKey}`,

src/domain/interfaces/repository/interface-purchasing-repo.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ export interface IPurchasingRepo {
8080
updateFilePurchasing(id?: number, user_id?: number, data?: Pick<PurchasingEntity, 'upload_file' | 'approval_file' | 'upload_file1' | 'upload_file2' | 'upload_file3' | 'upload_file4'>): Promise<any>
8181
updateFileSpbPurchasing(id?: number, user_id?: number, data?: Pick<PurchasingEntity, 'import2' | 'approval_file' | 'upload_file1' | 'upload_file2' | 'upload_file3' | 'upload_file4' | 'import2_date'>): Promise<any>
8282
addTerminKontrakPks(data?: PurchasingDetailEntity): Promise<any>
83-
findOneDynamicPurchasingDetail(conf?: Pick<ParamsEntity, "columnKey" | "columnValue">): Promise<PurchasingDetailEntity[] | []>
83+
findOneDynamicPurchasingDetail(conf?: Pick<ParamsEntity, "columnKey" | "columnValue" | "options">): Promise<PurchasingDetailEntity[] | []>
8484
checkQuantityTerminKontrakPks(type?: string, data?: TypePengajuanKontrakPks): Promise<any>
8585
updateTerminKontrakPks(id?: number, user_id?: number, data?: PurchasingDetailEntity): Promise<any>
8686
deleteTerminKontrakPks(id?: number, user_id?: number): Promise<any>

src/domain/interfaces/use-cases/interface-purchasing-use-case.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export interface IRegisterUserPurchasingUseCase {
55
}
66

77
export interface ILoginUserPurchasingUseCase {
8-
execute(data: EntityUser): Promise<EntityUser | null>
8+
execute(data: EntityUser): Promise<any>
99
}
1010

1111
export interface IChangedPasswordPurchasingUseCase {

src/domain/repository/purchasing/index.ts

+19-21
Original file line numberDiff line numberDiff line change
@@ -374,16 +374,12 @@ export class PurchasingRepository implements IPurchasingRepo {
374374
}
375375

376376
async pengajuanKontrakPks(user_id?: number, data?: TypePengajuanKontrakPks): Promise<any> {
377-
let pricePoPks: number = data?.price!
377+
378378
const resPurchasing = await this.purchasingDataSource.insert(data)
379379
const resPpn = await this.setupsDataSource.selectByNama(process.env.PPN_NAME)
380380

381-
382381
let jumlahPpn = Number(1) + Number(resPpn.nilai!)
383-
384-
if (data?.ppn == 1) {
385-
pricePoPks = Number(data?.price!) / Number(jumlahPpn)
386-
}
382+
let pricePoPks: number = data?.ppn == 1 ? Number(data?.price!) / Number(jumlahPpn) : data?.price!
387383

388384
let dataPoPks: PoPksEntity = {
389385
purchasing_id: resPurchasing[0].insertId,
@@ -415,24 +411,24 @@ export class PurchasingRepository implements IPurchasingRepo {
415411
if (tmpFreighCostId.length > 0)
416412
await Promise.all(
417413
tmpFreighCostId.map(async (val: number, i: number) => {
418-
let dataVendorKontrak: VendorKontrakEntity = {}
419-
let dataPurchasingFreightCost: PurchasingFreightCostEntity = {}
420-
dataVendorKontrak = {
414+
let dataVendorKontrak = new Map<string, VendorKontrakEntity>()
415+
let dataPurchasingFreightCost = new Map<string, PurchasingFreightCostEntity>()
416+
dataVendorKontrak.set('data', {
421417
freight_cost_id: val,
422418
po_pks_id: resPoPks[0].insertId,
423419
stockpile_contract_id: 0,
424420
quantity: data?.quantity,
425421
entry_date: data?.entry_date,
426422
entry_by: user_id,
427423
status: 1
428-
}
429-
dataPurchasingFreightCost = {
424+
})
425+
dataPurchasingFreightCost.set('data', {
430426
purchasing_id: resPurchasing[0].insertId,
431427
freight_cost_id: val,
432428
entry_date: `${format(new Date(), 'yyyy-MM-dd HH:mm:ss')}`
433-
}
434-
const resVendorKontrak = await this.vendorKontrakDataSource.insert(dataVendorKontrak)
435-
const resPurchasingFreightCost = await this.purchasingFreightCostDataSource.insert(dataPurchasingFreightCost)
429+
})
430+
const resVendorKontrak = await this.vendorKontrakDataSource.insert(dataVendorKontrak.get('data'))
431+
const resPurchasingFreightCost = await this.purchasingFreightCostDataSource.insert(dataPurchasingFreightCost.get('data'))
436432

437433
const dataHistoryLogVendorKontrak: HistoryLogEntity = {
438434
tanggal: `${format(new Date(), 'yyyy-MM-dd HH:mm:ss')}`,
@@ -474,25 +470,27 @@ export class PurchasingRepository implements IPurchasingRepo {
474470
tanggal: `${format(new Date(), 'yyyy-MM-dd HH:mm:ss')}`,
475471
user_id: user_id
476472
})
477-
const dataHistoryLogPurchasing: HistoryLogEntity = {
473+
const dataHistoryLogPurchasing = new Map<string, HistoryLogEntity>()
474+
dataHistoryLogPurchasing.set('data', {
478475
tanggal: `${format(new Date(), 'yyyy-MM-dd HH:mm:ss')}`,
479476
transaksi: `${resPurchasing[0].insertId}`,
480477
cud: 'CREATE',
481478
isitransaksi_baru: `MENGAJUKAN KONTRAK PKS ( table purchasing )`,
482479
user_id: user_id
483-
}
484-
const dataHistoryLogPoPks: HistoryLogEntity = {
480+
})
481+
const dataHistoryLogPoPks = new Map<string, HistoryLogEntity>()
482+
dataHistoryLogPoPks.set('data', {
485483
tanggal: `${format(new Date(), 'yyyy-MM-dd HH:mm:ss')}`,
486484
transaksi: `${resPoPks[0].insertId}`,
487485
cud: 'CREATE',
488486
isitransaksi_baru: `MENGAJUKAN KONTRAK PKS ( table po pks)`,
489487
user_id: user_id
490-
}
488+
})
491489

492490
await Promise.all(
493491
[
494-
this.historyLogDataSource.insert(dataHistoryLogPurchasing),
495-
this.historyLogDataSource.insert(dataHistoryLogPoPks),
492+
this.historyLogDataSource.insert(dataHistoryLogPurchasing.get('data')),
493+
this.historyLogDataSource.insert(dataHistoryLogPoPks.get('data')),
496494
this.historyLogDataSource.insert(dataLogPurchasingDetail.get('data'))
497495
]
498496
)
@@ -640,7 +638,7 @@ export class PurchasingRepository implements IPurchasingRepo {
640638
return insert
641639
}
642640

643-
async findOneDynamicPurchasingDetail(conf?: Pick<ParamsEntity, 'columnKey' | 'columnValue'> | undefined): Promise<PurchasingDetailEntity[] | []> {
641+
async findOneDynamicPurchasingDetail(conf?: Pick<ParamsEntity, 'columnKey' | 'columnValue' | 'options'> | undefined): Promise<PurchasingDetailEntity[] | []> {
644642
const res = await this.purchasingDetailDataSource.selectOneDynamic(conf!)
645643
return res
646644
}

src/domain/use-cases/purchasing/add-termin-kontrak-pks.ts

+22-18
Original file line numberDiff line numberDiff line change
@@ -10,32 +10,23 @@ export class AddTerminKontrakPksUseCase implements IAddTerminKontrakPksUseCase {
1010
}
1111

1212
async execute(data?: TypePengajuanKontrakPks | undefined): Promise<any> {
13-
data!.termin = 0
1413
const result = new Map<string, string | number | boolean | HttpResponse>()
1514
const dataCheckQuantityTermin = new Map<string, Pick<TypePengajuanKontrakPks, 'purchasing_id'>>()
1615
const paramsLastDataTermin = new Map<string, Pick<ParamsEntity, 'columnKey' | 'columnValue' | 'options'>>()
1716

18-
dataCheckQuantityTermin.set('data', {
19-
purchasing_id: data?.purchasing_id
20-
})
21-
const checkQuantityTermin = await this.purchasingRepo.checkQuantityTerminKontrakPks(undefined, dataCheckQuantityTermin.get('data'))
22-
const selisihQuantityTermin = Number(checkQuantityTermin.remain_quantity_termin) - Number(data?.quantity_payment)
23-
24-
if (selisihQuantityTermin < 0) {
25-
result.set('error', true)
26-
result.set('dataError', {
27-
message: 'Quantity melebihi dari ketentuan',
28-
status: false
29-
})
30-
return result
31-
}
32-
3317
paramsLastDataTermin.set('data', {
3418
columnKey: 'purchasing_id',
3519
columnValue: data?.purchasing_id,
3620
options: 'order by purchasing_detail_id desc limit 1'
3721
})
3822
const lastDataTermin = await this.purchasingRepo.findOneDynamicPurchasingDetail(paramsLastDataTermin.get('data'))
23+
if (!lastDataTermin) {
24+
result.set('error', true)
25+
result.set('dataError', {
26+
status: false,
27+
message: 'Maaf tidak ada data purchasing, anda tidak bisa menambahkan termin'
28+
})
29+
}
3930
if (lastDataTermin.length > 0 && !lastDataTermin[0]?.admin_input_by) {
4031
result.set('error', true)
4132
result.set('dataError', {
@@ -44,8 +35,21 @@ export class AddTerminKontrakPksUseCase implements IAddTerminKontrakPksUseCase {
4435
})
4536
return result
4637
}
47-
if (lastDataTermin[0]?.termin) {
48-
data!.termin++
38+
data!.termin = lastDataTermin[0]?.termin! + 1
39+
40+
dataCheckQuantityTermin.set('data', {
41+
purchasing_id: data?.purchasing_id
42+
})
43+
const checkQuantityTermin = await this.purchasingRepo.checkQuantityTerminKontrakPks(undefined, dataCheckQuantityTermin.get('data'))
44+
const selisihQuantityTermin = Number(checkQuantityTermin.remain_quantity_termin) - Number(data?.quantity_payment)
45+
46+
if (selisihQuantityTermin < 0) {
47+
result.set('error', true)
48+
result.set('dataError', {
49+
message: 'Quantity melebihi dari ketentuan',
50+
status: false
51+
})
52+
return result
4953
}
5054

5155
data!.entry_date = `${format(new Date(), 'yyyy-MM-dd HH:mm:ss')}`

src/domain/use-cases/purchasing/login-user-purchasing.ts

+33-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { EntityUser, ILoginUserPurchasingUseCase, IPurchasingRepo } from '../..'
1+
import { EntityUser, HttpResponse, ILoginUserPurchasingUseCase, IPurchasingRepo } from '../..'
22
import bcrypt from 'bcryptjs'
33
import { AppError } from '@jpj-common/module'
44

@@ -9,20 +9,36 @@ export class LoginUserPurchasingUseCase implements ILoginUserPurchasingUseCase {
99
this.purchasingRepo = purchasingRepo
1010
}
1111

12-
async execute(data: EntityUser): Promise<EntityUser | null> {
12+
async execute(data: EntityUser): Promise<any> {
1313
try {
14+
const result = new Map<string, boolean | HttpResponse>()
1415
let res = await this.purchasingRepo.checkDeviceId(data.deviced_id!)
1516

1617
if (res == null) {
17-
return null
18+
result.set('error', true)
19+
result.set('dataError', {
20+
status: false,
21+
message: 'Data anda tidak ada'
22+
})
23+
return result
1824
}
1925

2026
if (!res?.deviced_id) {
21-
return { deviced_id: 'Kosong' }
27+
result.set('error', true)
28+
result.set('dataError', {
29+
status: false,
30+
message: 'Data device id anda tidak ada'
31+
})
32+
return result
2233
}
2334

2435
if (res.deviced_id != data.deviced_id) {
25-
return { deviced_id: 'Tidak cocok' }
36+
result.set('error', true)
37+
result.set('dataError', {
38+
status: false,
39+
message: 'Device id anda tidak cocok'
40+
})
41+
return result
2642
}
2743

2844
let comparePassword = await bcrypt.compare(
@@ -31,10 +47,20 @@ export class LoginUserPurchasingUseCase implements ILoginUserPurchasingUseCase {
3147
)
3248

3349
if (!comparePassword) {
34-
return { kode_akses: 'Salah' }
50+
result.set('error', true)
51+
result.set('dataError', {
52+
status: false,
53+
message: 'Password anda salah'
54+
})
55+
return result
3556
}
3657

37-
return res
58+
result.set('dataSuccess', {
59+
status: true,
60+
message: 'Login Berhasil',
61+
data: res
62+
})
63+
return result
3864
} catch (error) {
3965
throw new AppError(400, false, `${error}`, '401')
4066
}

src/domain/use-cases/purchasing/pengajuan-kontrak-pks.ts

+14-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { AppError } from "@jpj-common/module";
22
import { format } from "date-fns";
3-
import { TypePengajuanKontrakPks } from "../../entity";
3+
import { HttpResponse, TypePengajuanKontrakPks } from "../../entity";
44
import { IPengajuanKontrakPksUseCase, IPurchasingRepo } from "../../interfaces";
55

66
export class PengajuanKontrakPksUseCase implements IPengajuanKontrakPksUseCase {
@@ -12,18 +12,25 @@ export class PengajuanKontrakPksUseCase implements IPengajuanKontrakPksUseCase {
1212

1313
async execute(user_id?: number, data?: TypePengajuanKontrakPks | undefined): Promise<any> {
1414
try {
15-
const result = new Map<string, any>()
15+
const result = new Map<string, string | number | boolean | HttpResponse>()
1616
data!.entry_by = user_id
1717
data!.entry_date = `${format(new Date(), 'yyyy-MM-dd HH:mm:ss')}`
1818

19-
if (data?.isTermin) {
20-
if (Number(data?.quantity) < Number(data?.quantity_payment)) {
21-
return result.set('remainQuantity', true)
22-
}
19+
if (data?.isTermin && Number(data?.quantity) < Number(data?.quantity_payment)) {
20+
result.set('error', true)
21+
result.set('dataError', {
22+
status: false,
23+
message: 'Maaf quantity termin melebihi quantity utama'
24+
})
25+
return result
2326
}
2427

2528
const res = await this.purchasingRepo.pengajuanKontrakPks(user_id, data)
26-
result.set('id', res[0].insertId)
29+
result.set('dataSuccess', {
30+
status: true,
31+
message: '',
32+
data: res[0].insertId
33+
})
2734

2835
return result
2936
} catch (error) {

src/domain/use-cases/purchasing/register-user-purchasing.ts

+20-5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
EntityUser,
44
IRegisterUserPurchasingUseCase,
55
IPurchasingRepo,
6+
HttpResponse,
67
} from '../..'
78
import bcryptjs from 'bcryptjs'
89

@@ -16,26 +17,40 @@ export class RegisterUserPurchasingUseCase
1617

1718
async execute(data: EntityUser): Promise<any> {
1819
try {
20+
const result = new Map<string, string | number | boolean | HttpResponse>()
1921
let saltPass = await bcryptjs.genSalt(5)
2022
let hashPass = await bcryptjs.hash(data.kode_akses!, saltPass)
2123
data!.kode_akses = hashPass
2224

2325
const checkEmail = await this.purchasingRepo.checkEmail(data.user_email!)
2426

2527
if (!checkEmail) {
26-
return { invalidEmail: 'Email tidak terdaftar' }
28+
result.set('error', true)
29+
result.set('dataError', {
30+
status: false,
31+
message: 'Email anda tidak terdaftar'
32+
})
33+
return result
2734
}
2835

2936
if (checkEmail.deviced_id == data.deviced_id) {
30-
return {
31-
existDeviceId: 'Device Id anda sudah terdaftar',
32-
}
37+
result.set('error', true)
38+
result.set('dataError', {
39+
status: false,
40+
message: 'Device Id anda sudah terdaftar'
41+
})
42+
return result
3343
}
3444

3545
data!.user_id = checkEmail.user_id
3646
const res = await this.purchasingRepo.registerUserPurchasing(data)
47+
result.set('dataSuccess', {
48+
status: true,
49+
message: 'Data deviced id berhasil di input',
50+
data: res[0].changedRows
51+
})
3752

38-
return res
53+
return result
3954
} catch (error) {
4055
throw new AppError(500, false, `${error}`, '501')
4156
}

0 commit comments

Comments
 (0)