Skip to content

Instantly share code, notes, and snippets.

@GoodnessEzeokafor
Created November 13, 2024 11:46
Show Gist options
  • Save GoodnessEzeokafor/6b7b9cf7a1cc1879304d95848472f867 to your computer and use it in GitHub Desktop.
Save GoodnessEzeokafor/6b7b9cf7a1cc1879304d95848472f867 to your computer and use it in GitHub Desktop.
Refactoring my transaction factory service
@Injectable()
export class TransactionFactoryServices {
constructor(public readonly dbUtils: DatabaseUtilsService) {}
private mapProperty(source: any, target: any, propertyName: string): void {
if (source[propertyName]) {
target[propertyName] = source[propertyName];
}
}
private mapNestedProperties(source: any, target: any, prefix?: string): void {
Object.keys(source).forEach((key) => {
const newKey = prefix ? `${prefix}.${key}` : key;
if (typeof source[key] === 'object' && source[key] !== null) {
if (!target[newKey]) {
target[newKey] = {};
}
this.mapNestedProperties(source[key], target[newKey], newKey);
} else {
this.mapProperty(source, target, newKey);
}
});
}
clean(data: Partial<TransactionEntity>) {
const saved: Partial<TransactionEntity> = {};
// Map top-level properties
['user', 'userId', 'amount', 'signedAmount', 'reference', 'description', 'appDescription', 'category', 'balanceBefore', 'balanceAfter'].forEach((prop) => {
this.mapProperty(data, saved, prop);
});
// Map wallet properties
['wallet', 'rewardsWallet', 'oneTimeRewardsWallet', 'generalReference'].forEach((prop) => {
this.mapProperty(data, saved, prop);
});
// Map biller information
['biller', 'billerLogo', 'meter', 'tariffDescription'].forEach((prop) => {
this.mapProperty(data, saved, prop);
});
// Map recipient information
['recipient', 'phone', 'recipientBankCode', 'recipientAccountNumber', 'recipientAccountName', 'recipientBankName', 'recipientBankLogo'].forEach((prop) => {
this.mapProperty(data, saved, prop);
});
// Map transaction details
['transRemark', 'narration', 'dotAiReference', 'dotAiRequestId', 'fee'].forEach((prop) => {
this.mapProperty(data, saved, prop);
});
// Map virtual account and currency
['virtualAccount', 'currency'].forEach((prop) => {
this.mapProperty(data, saved, prop);
});
// Map funded by admin and referral campaign
['fundedByAdminId', 'oneTimeReferralCampaignId'].forEach((prop) => {
this.mapProperty(data, saved, prop);
});
// Map internal description
['internalDescription'].forEach((prop) => {
this.mapProperty(data, saved, prop);
});
return saved;
}
cleanQuery(data: IGetAllTransactions) {
const key = {} as Partial<IGetAllTransactions>;
// ... rest of the method remains unchanged
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment