-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api): add alert endpoints for transfer check (#484)
- Loading branch information
Showing
7 changed files
with
545 additions
and
8 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,100 @@ | ||
import { | ||
type CreateTransferAlertDto, | ||
type TransferAlertDto, | ||
type UpdateTransferAlertDto, | ||
} from 'marble-api/generated/transfercheck-api'; | ||
|
||
export type TransferAlerStatus = 'unread' | 'read' | 'archived'; | ||
|
||
export type TransferAlertType = 'sender' | 'beneficiary'; | ||
|
||
export interface TransferAlert { | ||
id: string; | ||
type: TransferAlertType; | ||
transferId: string; | ||
senderPartnerId: string; | ||
beneficiaryPartnerId: string; | ||
createdAt: string; | ||
status: TransferAlerStatus; | ||
message: string; | ||
transferEndToEndId: string; | ||
beneficiaryIban: string; | ||
senderIban: string; | ||
} | ||
|
||
export function adaptTransferAlert( | ||
dto: TransferAlertDto, | ||
partnerId: string, | ||
): TransferAlert { | ||
let type: TransferAlertType; | ||
if (dto.sender_partner_id === partnerId) { | ||
type = 'sender'; | ||
} else if (dto.beneficiary_partner_id === partnerId) { | ||
type = 'beneficiary'; | ||
} else { | ||
throw new Error('Invalid partner id'); | ||
} | ||
return { | ||
id: dto.id, | ||
transferId: dto.transfer_id, | ||
type, | ||
senderPartnerId: dto.sender_partner_id, | ||
beneficiaryPartnerId: dto.beneficiary_partner_id, | ||
createdAt: dto.created_at, | ||
status: dto.status, | ||
message: dto.message, | ||
transferEndToEndId: dto.transfer_end_to_end_id, | ||
beneficiaryIban: dto.beneficiary_iban, | ||
senderIban: dto.sender_iban, | ||
}; | ||
} | ||
|
||
export interface CreateTransferAlert { | ||
transferId: string; | ||
message: string; | ||
transferEndToEndId: string; | ||
beneficiaryIban: string; | ||
senderIban: string; | ||
} | ||
|
||
export function adaptCreateTransferAlertDto( | ||
createTransferAlert: CreateTransferAlert, | ||
): CreateTransferAlertDto { | ||
return { | ||
transfer_id: createTransferAlert.transferId, | ||
message: createTransferAlert.message, | ||
transfer_end_to_end_id: createTransferAlert.transferEndToEndId, | ||
beneficiary_iban: createTransferAlert.beneficiaryIban, | ||
sender_iban: createTransferAlert.senderIban, | ||
}; | ||
} | ||
|
||
export type UpdateTransferAlert = | ||
| { | ||
type: 'sender'; | ||
message: string; | ||
transferEndToEndId: string; | ||
beneficiaryIban: string; | ||
senderIban: string; | ||
} | ||
| { | ||
type: 'beneficiary'; | ||
status: TransferAlerStatus; | ||
}; | ||
|
||
export function adaptUpdateTransferAlertDto( | ||
updateTransferAlert: UpdateTransferAlert, | ||
): UpdateTransferAlertDto { | ||
if (updateTransferAlert.type === 'sender') { | ||
return { | ||
message: updateTransferAlert.message, | ||
transfer_end_to_end_id: updateTransferAlert.transferEndToEndId, | ||
beneficiary_iban: updateTransferAlert.beneficiaryIban, | ||
sender_iban: updateTransferAlert.senderIban, | ||
}; | ||
} else { | ||
return { | ||
status: updateTransferAlert.status, | ||
}; | ||
} | ||
} |
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
66 changes: 66 additions & 0 deletions
66
packages/app-builder/src/repositories/TransferAlertRepository.ts
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,66 @@ | ||
import { type TransfercheckApi } from '@app-builder/infra/transfercheck-api'; | ||
import { | ||
adaptCreateTransferAlertDto, | ||
adaptTransferAlert, | ||
adaptUpdateTransferAlertDto, | ||
type CreateTransferAlert, | ||
type TransferAlert, | ||
type UpdateTransferAlert, | ||
} from '@app-builder/models/transfer-alert'; | ||
import invariant from 'tiny-invariant'; | ||
|
||
export interface TransferAlertRepository { | ||
listAlerts(): Promise<TransferAlert[]>; | ||
createAlert(args: CreateTransferAlert): Promise<TransferAlert>; | ||
getAlert(args: { alertId: string }): Promise<TransferAlert>; | ||
updateAlert( | ||
alertId: string, | ||
updateTransferAlert: UpdateTransferAlert, | ||
): Promise<TransferAlert>; | ||
} | ||
|
||
export function makeGetTransferAlertRepository() { | ||
return ( | ||
transfercheckApi: TransfercheckApi, | ||
// TODO: make partnerId required (need to split init.server.ts into multiple files) | ||
partnerId?: string, | ||
): TransferAlertRepository => ({ | ||
listAlerts: async () => { | ||
// TODO: make partnerId required (need to split init.server.ts into multiple files) | ||
invariant(partnerId, 'partnerId is required'); | ||
|
||
const { alerts } = await transfercheckApi.listAlerts(); | ||
|
||
return alerts.map((alert) => adaptTransferAlert(alert, partnerId)); | ||
}, | ||
createAlert: async (createTransferAlert) => { | ||
// TODO: make partnerId required (need to split init.server.ts into multiple files) | ||
invariant(partnerId, 'partnerId is required'); | ||
|
||
const { alert } = await transfercheckApi.createAlert( | ||
adaptCreateTransferAlertDto(createTransferAlert), | ||
); | ||
|
||
return adaptTransferAlert(alert, partnerId); | ||
}, | ||
getAlert: async ({ alertId }) => { | ||
// TODO: make partnerId required (need to split init.server.ts into multiple files) | ||
invariant(partnerId, 'partnerId is required'); | ||
|
||
const { alert } = await transfercheckApi.getAlert(alertId); | ||
|
||
return adaptTransferAlert(alert, partnerId); | ||
}, | ||
updateAlert: async (alertId, updateTransferAlert) => { | ||
// TODO: make partnerId required (need to split init.server.ts into multiple files) | ||
invariant(partnerId, 'partnerId is required'); | ||
|
||
const { alert } = await transfercheckApi.updateAlert( | ||
alertId, | ||
adaptUpdateTransferAlertDto(updateTransferAlert), | ||
); | ||
|
||
return adaptTransferAlert(alert, partnerId); | ||
}, | ||
}); | ||
} |
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
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
Oops, something went wrong.