-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
86084a7
commit 40a51df
Showing
4 changed files
with
225 additions
and
1 deletion.
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
50 changes: 50 additions & 0 deletions
50
src/adapters/donationSaveBackup/donationSaveBackupAdapter.test.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,50 @@ | ||
// test calling the donationSaveBackupAdapter get getDonationsFromBackup method | ||
|
||
import { assert } from 'chai'; | ||
import { | ||
getNotImportedDonationsFromBackup, | ||
getSingleDonationFromBackupById, | ||
getSingleDonationFromBackupByTxHash, | ||
markDonationAsImported, | ||
unmarkDonationAsImported, | ||
} from './donationSaveBackupAdapter'; | ||
|
||
describe('Donation Save Backup Adapter Test Cases', () => { | ||
it('should return a list of donations', async () => { | ||
const result = await getNotImportedDonationsFromBackup(2); | ||
assert.lengthOf(result, 2); | ||
}); | ||
|
||
it('get a single donation by id', async () => { | ||
const result = await getNotImportedDonationsFromBackup(1); | ||
const singleResult = await getSingleDonationFromBackupById(result[0]._id); | ||
assert.ok(singleResult); | ||
assert.equal(singleResult._id, result[0]._id); | ||
}); | ||
|
||
it('get single donation by txHash', async () => { | ||
const result = await getNotImportedDonationsFromBackup(1); | ||
const singleResult = await getSingleDonationFromBackupByTxHash( | ||
result[0].txHash, | ||
); | ||
assert.ok(singleResult); | ||
assert.equal(singleResult._id, result[0]._id); | ||
}); | ||
|
||
it('should mark a donation as imported', async function () { | ||
const result = await getNotImportedDonationsFromBackup(1); | ||
|
||
if (result.length === 0) { | ||
// tslint:disable-next-line:no-console | ||
console.log('no donations to test'); | ||
this.skip(); | ||
} | ||
|
||
await markDonationAsImported(result[0]._id); | ||
let newObject = await getSingleDonationFromBackupById(result[0]._id); | ||
assert.isTrue(newObject.imported); | ||
await unmarkDonationAsImported(result[0]._id); | ||
newObject = await getSingleDonationFromBackupById(result[0]._id); | ||
assert.isNotTrue(newObject.imported); | ||
}); | ||
}); |
169 changes: 169 additions & 0 deletions
169
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,169 @@ | ||
// 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'; | ||
|
||
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; | ||
|
||
// 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 const getNotImportedDonationsFromBackup = async ( | ||
limit = 10, | ||
skip = 0, | ||
) => { | ||
const result = await axios.post( | ||
new URL('find', baseUrl).href, | ||
{ | ||
collection: 'failed_donation', | ||
database: 'failed_donation', | ||
dataSource: 'staging-giveth-donation-backupservice', | ||
limit, | ||
skip, | ||
filter: { | ||
imported: { $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('getDonationsFromBackup error', result.data); | ||
throw new Error('getDonationsFromBackup error'); | ||
} | ||
return result.data.documents; | ||
}; | ||
|
||
export const getSingleDonationFromBackupByTxHash = async (txHash: string) => { | ||
const result = await axios.post( | ||
new URL('findOne', baseUrl).href, | ||
{ | ||
collection: 'failed_donation', | ||
database: 'failed_donation', | ||
dataSource: 'staging-giveth-donation-backupservice', | ||
filter: { | ||
txHash, | ||
}, | ||
}, | ||
{ | ||
headers: { | ||
'api-key': DONATION_SAVE_BACKUP_API_SECRET, | ||
'Content-Type': 'application/json', | ||
'Access-Control-Request-Headers': '*', | ||
}, | ||
}, | ||
); | ||
|
||
if (result.status !== 200) { | ||
logger.error('getDonationsFromBackup error', result.data); | ||
throw new Error('getDonationsFromBackup error'); | ||
} | ||
return result.data.document; | ||
}; | ||
export const getSingleDonationFromBackupById = async (id: string) => { | ||
const result = await axios.post( | ||
new URL('findOne', baseUrl).href, | ||
{ | ||
collection: 'failed_donation', | ||
database: 'failed_donation', | ||
dataSource: 'staging-giveth-donation-backupservice', | ||
filter: { | ||
_id: { $oid: id }, | ||
}, | ||
}, | ||
{ | ||
headers: { | ||
'api-key': DONATION_SAVE_BACKUP_API_SECRET, | ||
'Content-Type': 'application/json', | ||
'Access-Control-Request-Headers': '*', | ||
}, | ||
}, | ||
); | ||
|
||
if (result.status !== 200) { | ||
logger.error('getDonationsFromBackup error', result.data); | ||
throw new Error('getDonationsFromBackup error'); | ||
} | ||
return result.data.document; | ||
}; | ||
|
||
export const markDonationAsImported = async (id: string) => { | ||
const result = await axios.post( | ||
new URL('updateOne', baseUrl).href, | ||
{ | ||
collection: 'failed_donation', | ||
database: 'failed_donation', | ||
dataSource: 'staging-giveth-donation-backupservice', | ||
filter: { | ||
_id: { $oid: id }, | ||
}, | ||
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('getDonationsFromBackup error', result.data); | ||
throw new Error('getDonationsFromBackup error'); | ||
} | ||
return result; | ||
}; | ||
|
||
export const unmarkDonationAsImported = async (id: string) => { | ||
const result = await axios.post( | ||
new URL('updateOne', baseUrl).href, | ||
{ | ||
collection: 'failed_donation', | ||
database: 'failed_donation', | ||
dataSource: 'staging-giveth-donation-backupservice', | ||
filter: { | ||
_id: { $oid: id }, | ||
}, | ||
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('getDonationsFromBackup error', result.data); | ||
throw new Error('getDonationsFromBackup error'); | ||
} | ||
return result; | ||
}; |