Skip to content

Commit

Permalink
fix: date check if user has already claimed the credential
Browse files Browse the repository at this point in the history
Signed-off-by: Urban Vidovič <[email protected]>
  • Loading branch information
pseudobun committed Mar 30, 2024
1 parent 788b2d6 commit 4fb49e0
Showing 1 changed file with 61 additions and 42 deletions.
103 changes: 61 additions & 42 deletions packages/dapp/src/app/api/campaigns/issue/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,35 +76,74 @@ export async function POST(request: NextRequest) {
});
}

if (campaign.total && campaign.claimed >= campaign.total) {
return new NextResponse('Campaign is already fully claimed', {
status: 400,
headers: {
...CORS_HEADERS,
},
});
}
const { data: completedRequirements, error: completedRequirementsError } =
await supabase.rpc('get_num_of_users_requirements_by_campaign', {
campaign_id: campaignId,
user_id: user.sub,
});
const { data: claim, error: claimError } = await supabase
.from('claims')
.select('*')
.eq('campaign_id', campaignId)
.eq('user_id', user.sub);

if (completedRequirementsError) {
if (claimError) {
return new NextResponse('Internal Server Error', {
status: 500,
headers: {
...CORS_HEADERS,
},
});
}
if (completedRequirements !== campaign.requirements.length) {
return new NextResponse('User has not completed all requirements', {
status: 400,
headers: {
...CORS_HEADERS,
},
});

let claimDate = new Date().toISOString();
let alreadyClaimed = false;
if (claim.length > 0) {
claimDate = claim[0].claimed_at;
alreadyClaimed = true;
}

if (!alreadyClaimed) {
const now = new Date();
const startDateValid =
campaign.start_date && new Date(campaign.start_date) > now;
const endDateValid =
campaign.end_date && new Date(campaign.end_date) < now;
const isCampaignInactive = startDateValid || endDateValid;
const isCampaignFullyClaimed =
campaign.total && campaign.claimed >= campaign.total;

if (isCampaignInactive) {
return new NextResponse('Campaign is not active', {
status: 400,
headers: { ...CORS_HEADERS },
});
}

if (isCampaignFullyClaimed) {
return new NextResponse('Campaign is already fully claimed', {
status: 400,
headers: { ...CORS_HEADERS },
});
}

const { data: completedRequirements, error: completedRequirementsError } =
await supabase.rpc('get_num_of_users_requirements_by_campaign', {
campaign_id: campaignId,
user_id: user.sub,
});

if (completedRequirementsError) {
return new NextResponse('Internal Server Error', {
status: 500,
headers: {
...CORS_HEADERS,
},
});
}
if (completedRequirements !== campaign.requirements.length) {
return new NextResponse('User has not completed all requirements', {
status: 400,
headers: {
...CORS_HEADERS,
},
});
}
}

const agent = await getAgent();
Expand Down Expand Up @@ -135,21 +174,6 @@ export async function POST(request: NextRequest) {
});
}

const { data: claim, error: claimError } = await supabase
.from('claims')
.select('*')
.eq('campaign_id', campaignId)
.eq('user_id', user.sub);

if (claimError) {
return new NextResponse('Internal Server Error', {
status: 500,
headers: {
...CORS_HEADERS,
},
});
}

const controllerKeyId = 'key-1';

const issuerDid = await agent.didManagerImport({
Expand All @@ -166,12 +190,8 @@ export async function POST(request: NextRequest) {
],
});

let claimDate = new Date().toISOString();
if (claim.length > 0) {
claimDate = claim[0].claimed_at;
}
let credentialId = claim[0]?.credential_id;
if (claim.length === 0) {
if (!alreadyClaimed) {
const { data: insertedClaimData, error: updatedClaimError } =
await supabase
.from('claims')
Expand All @@ -194,7 +214,6 @@ export async function POST(request: NextRequest) {
});
}

console.error('Error updating claim', updatedClaimError);
return new NextResponse('Internal Server Error', {
status: 500,
headers: {
Expand Down

0 comments on commit 4fb49e0

Please sign in to comment.