Skip to content

Commit

Permalink
Make batch minting eligible users api backward compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
aminlatifi committed Dec 9, 2024
1 parent 10f179f commit ab53e2c
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 15 deletions.
73 changes: 59 additions & 14 deletions src/resolvers/userResolver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
import {
acceptedTermsOfService,
batchMintingEligibleUsers,
batchMintingEligibleUsersV2,
checkUserPrivadoVerifiedState,
refreshUserScores,
updateUser,
Expand Down Expand Up @@ -63,6 +64,10 @@ describe(
'batchMintingEligibleUsers() test cases',
batchMintingEligibleUsersTestCases,
);
describe(
'batchMintingEligibleV2Users() test cases',
batchMintingEligibleUsersV2TestCases,
);

// TODO I think we can delete addUserVerification query
// describe('addUserVerification() test cases', addUserVerificationTestCases);
Expand Down Expand Up @@ -1184,13 +1189,53 @@ function batchMintingEligibleUsersTestCases() {
// clear all users not empty accepted terms of service
await User.delete({ acceptedToS: true });
});
it('should return users who have accepted terms of service and privado verified', async () => {
const user1 = await saveUserDirectlyToDb(generateRandomEtheriumAddress(), {
privadoVerifiedRequestIds: [PrivadoAdapter.privadoRequestId],
acceptedToS: true,
// 2 days ago
acceptedToSDate: new Date(Date.now() - DAY * 3),
});

const user2 = await saveUserDirectlyToDb(generateRandomEtheriumAddress(), {
privadoVerifiedRequestIds: [PrivadoAdapter.privadoRequestId],
acceptedToS: true,
// yesterday
acceptedToSDate: new Date(Date.now() - DAY * 2),
});

const user3 = await saveUserDirectlyToDb(generateRandomEtheriumAddress(), {
privadoVerifiedRequestIds: [PrivadoAdapter.privadoRequestId],
acceptedToS: true,
// yesterday
acceptedToSDate: new Date(Date.now() - DAY),
});

it('should return empty array if there is no user to mint', async () => {
const result = await axios.post(graphqlUrl, {
query: batchMintingEligibleUsers,
});

assert.deepEqual(result.data.data.batchMintingEligibleUsers.users, []);
assert.deepEqual(result.data.data.batchMintingEligibleUsers.users, [
user1.walletAddress,
user2.walletAddress,
user3.walletAddress,
]);
});
}

function batchMintingEligibleUsersV2TestCases() {
const DAY = 86400000;
beforeEach(async () => {
// clear all users not empty accepted terms of service
await User.delete({ acceptedToS: true });
});

it('should return empty array if there is no user to mint', async () => {
const result = await axios.post(graphqlUrl, {
query: batchMintingEligibleUsersV2,
});

assert.deepEqual(result.data.data.batchMintingEligibleUsersV2.users, []);
});

it('should return users who have accepted terms of service and has valid kyc status', async () => {
Expand All @@ -1216,10 +1261,10 @@ function batchMintingEligibleUsersTestCases() {
});

const result = await axios.post(graphqlUrl, {
query: batchMintingEligibleUsers,
query: batchMintingEligibleUsersV2,
});

assert.deepEqual(result.data.data.batchMintingEligibleUsers.users, [
assert.deepEqual(result.data.data.batchMintingEligibleUsersV2.users, [
{ address: user1.walletAddress, kycType: UserKycType.zkId },
{ address: user2.walletAddress, kycType: UserKycType.GTCPass },
{ address: user3.walletAddress, kycType: UserKycType.GTCPass },
Expand All @@ -1238,10 +1283,10 @@ function batchMintingEligibleUsersTestCases() {
});

const result = await axios.post(graphqlUrl, {
query: batchMintingEligibleUsers,
query: batchMintingEligibleUsersV2,
});

assert.deepEqual(result.data.data.batchMintingEligibleUsers.users, [
assert.deepEqual(result.data.data.batchMintingEligibleUsersV2.users, [
{ address: user.walletAddress, kycType: UserKycType.zkId },
]);
});
Expand All @@ -1262,10 +1307,10 @@ function batchMintingEligibleUsersTestCases() {
});

const result = await axios.post(graphqlUrl, {
query: batchMintingEligibleUsers,
query: batchMintingEligibleUsersV2,
});

assert.deepEqual(result.data.data.batchMintingEligibleUsers.users, [
assert.deepEqual(result.data.data.batchMintingEligibleUsersV2.users, [
{ address: user1.walletAddress, kycType: UserKycType.zkId },
]);
});
Expand All @@ -1290,27 +1335,27 @@ function batchMintingEligibleUsersTestCases() {
});

let result = await axios.post(graphqlUrl, {
query: batchMintingEligibleUsers,
query: batchMintingEligibleUsersV2,
variables: {
limit: 2,
skip: 0,
},
});

assert.deepEqual(result.data.data.batchMintingEligibleUsers.users, [
assert.deepEqual(result.data.data.batchMintingEligibleUsersV2.users, [
{ address: user1.walletAddress, kycType: UserKycType.zkId },
{ address: user2.walletAddress, kycType: UserKycType.GTCPass },
]);

result = await axios.post(graphqlUrl, {
query: batchMintingEligibleUsers,
query: batchMintingEligibleUsersV2,
variables: {
limit: 2,
skip: 2,
},
});

assert.deepEqual(result.data.data.batchMintingEligibleUsers.users, [
assert.deepEqual(result.data.data.batchMintingEligibleUsersV2.users, [
{ address: user3.walletAddress, kycType: UserKycType.GTCPass },
]);
});
Expand Down Expand Up @@ -1357,10 +1402,10 @@ function batchMintingEligibleUsersTestCases() {
});

const result = await axios.post(graphqlUrl, {
query: batchMintingEligibleUsers,
query: batchMintingEligibleUsersV2,
});

assert.deepEqual(result.data.data.batchMintingEligibleUsers.users, [
assert.deepEqual(result.data.data.batchMintingEligibleUsersV2.users, [
{ address: user1.walletAddress, kycType: UserKycType.zkId },
{ address: user2.walletAddress, kycType: UserKycType.zkId },
{ address: user3.walletAddress, kycType: UserKycType.zkId },
Expand Down
42 changes: 42 additions & 0 deletions src/resolvers/userResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,17 @@ class EligibleUser {

@ObjectType()
class BatchMintingEligibleUserResponse {
@Field(_addresses => [String], { nullable: false })
users: string[];

@Field(_total => Number, { nullable: false })
total: number;

@Field(_offset => Number, { nullable: false })
skip: number;
}
@ObjectType()
class BatchMintingEligibleUserV2Response {
@Field(_addresses => [EligibleUser], { nullable: false })
users: EligibleUser[];

Expand Down Expand Up @@ -174,6 +185,37 @@ export class UserResolver {
}

@Query(_returns => BatchMintingEligibleUserResponse)
async batchMintingEligibleUsers(
@Arg('limit', _type => Int, { nullable: true }) limit: number = 1000,
@Arg('skip', _type => Int, { nullable: true }) skip: number = 0,
@Arg('filterAddress', { nullable: true }) filterAddress: string,
) {
let query = User.createQueryBuilder('user')
.select('user.walletAddress')
.where('user.acceptedToS = true')
.andWhere(':privadoRequestId = ANY (user.privadoVerifiedRequestIds)', {
privadoRequestId: PrivadoAdapter.privadoRequestId,
});
if (filterAddress) {
query = query.andWhere(`LOWER("walletAddress") = :walletAddress`, {
walletAddress: filterAddress.toLowerCase(),
});
}

const response = await query
.orderBy('user.acceptedToSDate', 'ASC')
.take(limit)
.skip(skip)
.getManyAndCount();

return {
users: response[0].map((user: User) => user.walletAddress),
total: response[1],
skip,
};
}

@Query(_returns => BatchMintingEligibleUserV2Response)
async batchMintingEligibleUsersV2(
@Arg('limit', _type => Int, { nullable: true }) limit: number = 1000,
@Arg('skip', _type => Int, { nullable: true }) skip: number = 0,
Expand Down
12 changes: 11 additions & 1 deletion test/graphqlQueries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2120,7 +2120,17 @@ export const acceptedTermsOfService = `

export const batchMintingEligibleUsers = `
query ( $limit: Int, $skip: Int, $filterAddress: String) {
batchMintingEligibleUsers: batchMintingEligibleUsersV2(limit: $limit, skip: $skip, filterAddress: $filterAddress) {
batchMintingEligibleUsers(limit: $limit, skip: $skip, filterAddress: $filterAddress) {
users
total
skip
}
}
`;

export const batchMintingEligibleUsersV2 = `
query ( $limit: Int, $skip: Int, $filterAddress: String) {
batchMintingEligibleUsersV2(limit: $limit, skip: $skip, filterAddress: $filterAddress) {
users {
address
kycType
Expand Down

0 comments on commit ab53e2c

Please sign in to comment.