-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Import from Donation Backup Service (#1253)
* Added Donation Save Backup Adapter * add backup service import cronjob * add interface of used params from mongo data * Change loading urls for backup service * Refactor donationSaveBackupAdapter and adding mock adapter * Removed unused package script * Refactored createBackupDonation to reuse resolver * Fix createBackupDonation() and write test case for that * Add importError to failed donation mongo backup --------- Co-authored-by: mohammadranjbarz <[email protected]> Co-authored-by: Carlos <[email protected]>
- Loading branch information
1 parent
a00ed03
commit de14c4b
Showing
15 changed files
with
548 additions
and
4 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
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
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
43 changes: 43 additions & 0 deletions
43
src/adapters/donationSaveBackup/DonationSaveBackupInterface.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,43 @@ | ||
export type FetchedSavedFailDonationInterface = { | ||
_id: string; | ||
txHash: string; | ||
imported?: boolean; | ||
importError?: string; | ||
token: { | ||
symbol: string; | ||
address: string; | ||
networkId: number; | ||
}; | ||
walletAddress: string; | ||
amount: number; | ||
chainId: number; | ||
projectId: number; | ||
anonymous: boolean; | ||
nonce: number; | ||
symbol: string; | ||
chainvineReferred?: string; | ||
safeTransactionId?: string; | ||
}; | ||
|
||
export interface DonationSaveBackupInterface { | ||
getNotImportedDonationsFromBackup(params: { | ||
limit: number; | ||
}): Promise<FetchedSavedFailDonationInterface[]>; | ||
|
||
getSingleDonationFromBackupByTxHash( | ||
txHash: string, | ||
): Promise<FetchedSavedFailDonationInterface | null>; | ||
|
||
markDonationAsImported(donationMongoId: string): Promise<void>; | ||
|
||
unmarkDonationAsImported(donationMongoId: string): Promise<void>; | ||
|
||
getSingleDonationFromBackupById( | ||
donationMongoId: string, | ||
): Promise<FetchedSavedFailDonationInterface | null>; | ||
|
||
markDonationAsImportError( | ||
donationMongoId: string, | ||
errorMessage, | ||
): Promise<void>; | ||
} |
41 changes: 41 additions & 0 deletions
41
src/adapters/donationSaveBackup/DonationSaveBackupMockAdapter.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,41 @@ | ||
import { | ||
DonationSaveBackupInterface, | ||
FetchedSavedFailDonationInterface, | ||
} from './DonationSaveBackupInterface'; | ||
|
||
export class DonationSaveBackupMockAdapter | ||
implements DonationSaveBackupInterface | ||
{ | ||
async getNotImportedDonationsFromBackup(params: { | ||
limit: number; | ||
}): Promise<FetchedSavedFailDonationInterface[]> { | ||
return []; | ||
} | ||
|
||
async getSingleDonationFromBackupByTxHash( | ||
txHash: string, | ||
): Promise<FetchedSavedFailDonationInterface | null> { | ||
return null; | ||
} | ||
|
||
async markDonationAsImported(donationMongoId: string): Promise<void> { | ||
// | ||
} | ||
|
||
async unmarkDonationAsImported(donationMongoId: string): Promise<void> { | ||
// | ||
} | ||
|
||
async getSingleDonationFromBackupById( | ||
donationMongoId: string, | ||
): Promise<FetchedSavedFailDonationInterface | null> { | ||
return null; | ||
} | ||
|
||
markDonationAsImportError( | ||
donationMongoId: string, | ||
errorMessage, | ||
): Promise<void> { | ||
return Promise.resolve(undefined); | ||
} | ||
} |
218 changes: 218 additions & 0 deletions
218
src/adapters/donationSaveBackup/donationSaveBackupAdapter.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,218 @@ | ||
// a method the get objects from mongodb api read from config DONATION_SAVE_BACKUP_API_URL with sercret read from DONATION_SAVE_BACKUP_API_SECRET, | ||
// it must filter objects by those doesn't have `imported` field with true value | ||
// also must support pagination | ||
|
||
import { logger } from '../../utils/logger'; | ||
import config from '../../config'; | ||
import axios from 'axios'; | ||
import { | ||
DonationSaveBackupInterface, | ||
FetchedSavedFailDonationInterface, | ||
} from './DonationSaveBackupInterface'; | ||
|
||
const DONATION_SAVE_BACKUP_API_URL = config.get( | ||
'DONATION_SAVE_BACKUP_API_URL', | ||
) as string; | ||
const DONATION_SAVE_BACKUP_API_SECRET = config.get( | ||
'DONATION_SAVE_BACKUP_API_SECRET', | ||
) as string; | ||
const DONATION_SAVE_BACKUP_DATA_SOURCE = config.get( | ||
'DONATION_SAVE_BACKUP_DATA_SOURCE', | ||
) as string; | ||
const DONATION_SAVE_BACKUP_COLLECTION = | ||
config.get('DONATION_SAVE_BACKUP_COLLECTION') || 'failed_donation'; | ||
const DONATION_SAVE_BACKUP_DATABASE = | ||
config.get('DONATION_SAVE_BACKUP_DATABASE') || 'failed_donation'; | ||
|
||
// add '/' if doesn't exist at the | ||
const baseUrl = DONATION_SAVE_BACKUP_API_URL.endsWith('/') | ||
? DONATION_SAVE_BACKUP_API_URL | ||
: `${DONATION_SAVE_BACKUP_API_URL}/`; | ||
|
||
export class DonationSaveBackupAdapter implements DonationSaveBackupInterface { | ||
async getNotImportedDonationsFromBackup(params: { | ||
limit: number; | ||
}): Promise<FetchedSavedFailDonationInterface[]> { | ||
const result = await axios.post( | ||
`${baseUrl}find`, | ||
{ | ||
collection: DONATION_SAVE_BACKUP_COLLECTION, | ||
database: DONATION_SAVE_BACKUP_DATABASE, | ||
dataSource: DONATION_SAVE_BACKUP_DATA_SOURCE, | ||
limit: params.limit, | ||
filter: { | ||
imported: { $ne: true }, | ||
importError: { $ne: true }, | ||
}, | ||
sort: { _id: 1 }, | ||
}, | ||
{ | ||
headers: { | ||
'api-key': DONATION_SAVE_BACKUP_API_SECRET, | ||
'Content-Type': 'application/json', | ||
'Access-Control-Request-Headers': '*', | ||
}, | ||
}, | ||
); | ||
|
||
if (result.status !== 200) { | ||
logger.error('getNotImportedDonationsFromBackup error', result.data); | ||
throw new Error('getNotImportedDonationsFromBackup error'); | ||
} | ||
return result.data.documents; | ||
} | ||
|
||
async getSingleDonationFromBackupByTxHash( | ||
txHash: string, | ||
): Promise<FetchedSavedFailDonationInterface | null> { | ||
const result = await axios.post( | ||
`${baseUrl}findOne`, | ||
{ | ||
collection: DONATION_SAVE_BACKUP_COLLECTION, | ||
database: DONATION_SAVE_BACKUP_DATABASE, | ||
dataSource: DONATION_SAVE_BACKUP_DATA_SOURCE, | ||
filter: { | ||
txHash, | ||
}, | ||
}, | ||
{ | ||
headers: { | ||
'api-key': DONATION_SAVE_BACKUP_API_SECRET, | ||
'Content-Type': 'application/json', | ||
'Access-Control-Request-Headers': '*', | ||
}, | ||
}, | ||
); | ||
|
||
if (result.status !== 200) { | ||
logger.error('getSingleDonationFromBackupByTxHash error', result.data); | ||
throw new Error('getSingleDonationFromBackupByTxHash error'); | ||
} | ||
return result.data.document; | ||
} | ||
|
||
async markDonationAsImported(donationMongoId: string): Promise<void> { | ||
const result = await axios.post( | ||
`${baseUrl}updateOne`, | ||
{ | ||
collection: DONATION_SAVE_BACKUP_COLLECTION, | ||
database: DONATION_SAVE_BACKUP_DATABASE, | ||
dataSource: DONATION_SAVE_BACKUP_DATA_SOURCE, | ||
filter: { | ||
_id: { $oid: donationMongoId }, | ||
}, | ||
update: { | ||
$set: { | ||
imported: true, | ||
}, | ||
}, | ||
}, | ||
{ | ||
headers: { | ||
'api-key': DONATION_SAVE_BACKUP_API_SECRET, | ||
'Content-Type': 'application/json', | ||
'Access-Control-Request-Headers': '*', | ||
}, | ||
}, | ||
); | ||
|
||
if (result.status !== 200) { | ||
logger.error('markDonationAsImported error', result.data); | ||
throw new Error('markDonationAsImported error'); | ||
} | ||
} | ||
|
||
async markDonationAsImportError( | ||
donationMongoId: string, | ||
errorMessage, | ||
): Promise<void> { | ||
const result = await axios.post( | ||
`${baseUrl}updateOne`, | ||
{ | ||
collection: DONATION_SAVE_BACKUP_COLLECTION, | ||
database: DONATION_SAVE_BACKUP_DATABASE, | ||
dataSource: DONATION_SAVE_BACKUP_DATA_SOURCE, | ||
filter: { | ||
_id: { $oid: donationMongoId }, | ||
}, | ||
update: { | ||
$set: { | ||
importError: errorMessage, | ||
}, | ||
}, | ||
}, | ||
{ | ||
headers: { | ||
'api-key': DONATION_SAVE_BACKUP_API_SECRET, | ||
'Content-Type': 'application/json', | ||
'Access-Control-Request-Headers': '*', | ||
}, | ||
}, | ||
); | ||
|
||
if (result.status !== 200) { | ||
logger.error('markDonationAsImportError error', result.data); | ||
throw new Error('markDonationAsImportError error'); | ||
} | ||
} | ||
|
||
async getSingleDonationFromBackupById( | ||
donationMongoId: string, | ||
): Promise<FetchedSavedFailDonationInterface | null> { | ||
const result = await axios.post( | ||
`${baseUrl}findOne`, | ||
{ | ||
collection: DONATION_SAVE_BACKUP_COLLECTION, | ||
database: DONATION_SAVE_BACKUP_DATABASE, | ||
dataSource: DONATION_SAVE_BACKUP_DATA_SOURCE, | ||
filter: { | ||
_id: { $oid: donationMongoId }, | ||
}, | ||
}, | ||
{ | ||
headers: { | ||
'api-key': DONATION_SAVE_BACKUP_API_SECRET, | ||
'Content-Type': 'application/json', | ||
'Access-Control-Request-Headers': '*', | ||
}, | ||
}, | ||
); | ||
|
||
if (result.status !== 200) { | ||
logger.error('getSingleDonationFromBackupById error', result.data); | ||
throw new Error('getSingleDonationFromBackupById error'); | ||
} | ||
return result.data.document; | ||
} | ||
|
||
async unmarkDonationAsImported(donationMongoId: string): Promise<void> { | ||
const result = await axios.post( | ||
`${baseUrl}updateOne`, | ||
{ | ||
collection: DONATION_SAVE_BACKUP_COLLECTION, | ||
database: DONATION_SAVE_BACKUP_DATABASE, | ||
dataSource: DONATION_SAVE_BACKUP_DATA_SOURCE, | ||
filter: { | ||
_id: { $oid: donationMongoId }, | ||
}, | ||
update: { | ||
$unset: { | ||
imported: '', | ||
}, | ||
}, | ||
}, | ||
{ | ||
headers: { | ||
'api-key': DONATION_SAVE_BACKUP_API_SECRET, | ||
'Content-Type': 'application/json', | ||
'Access-Control-Request-Headers': '*', | ||
}, | ||
}, | ||
); | ||
|
||
if (result.status !== 200) { | ||
logger.error('unmarkDonationAsImported error', result.data); | ||
throw new Error('unmarkDonationAsImported error'); | ||
} | ||
} | ||
} |
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.