Skip to content

Commit

Permalink
Adds IP Limit refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
peterpolman committed Aug 7, 2024
1 parent 6343794 commit 1cc113a
Show file tree
Hide file tree
Showing 64 changed files with 361 additions and 741 deletions.
4 changes: 2 additions & 2 deletions apps/api/scripts/script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import db from '@thxnetwork/api/util/database';
// import main from './src/sdk';
// import main from './src/vethx';
// import main from './src/ipfs';
import main from './src/invoices';
// import main from './src/demo';
// import main from './src/invoices';
import main from './src/demo';
// import main from './src/preview';
// import main from './src/metamask';
// import main from './src/lottery';
Expand Down
1 change: 1 addition & 0 deletions apps/api/src/app/controllers/pools/get.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { param } from 'express-validator';
import { Event, Participant, Widget, Wallet, Webhook } from '@thxnetwork/api/models';
import PoolService from '@thxnetwork/api/services/PoolService';
import BrandService from '@thxnetwork/api/services/BrandService';
import QuestService from '@thxnetwork/api/services/QuestService';

const validation = [param('id').isMongoId()];

Expand Down
16 changes: 0 additions & 16 deletions apps/api/src/app/controllers/quests/custom/custom.router.ts

This file was deleted.

82 changes: 0 additions & 82 deletions apps/api/src/app/controllers/quests/custom/custom.test.ts

This file was deleted.

This file was deleted.

16 changes: 0 additions & 16 deletions apps/api/src/app/controllers/quests/daily/daily.router.ts

This file was deleted.

This file was deleted.

59 changes: 59 additions & 0 deletions apps/api/src/app/controllers/quests/entries/post.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { Request, Response } from 'express';
import { body, param } from 'express-validator';
import { NotFoundError } from '@thxnetwork/api/util/errors';
import { JobType, QuestVariant } from '@thxnetwork/common/enums';
import { agenda } from '@thxnetwork/api/util/agenda';
import QuestService from '@thxnetwork/api/services/QuestService';
import { questInteractionVariantMap } from '@thxnetwork/common/maps/quest';
import { QuestSocial } from '@thxnetwork/api/models';

const validation = [param('variant').isString(), param('id').isMongoId(), body('recaptcha').isString()];

// Dynamically create a map from string to enum value
const stringToEnumMap = Object.keys(QuestVariant)
.filter((key) => isNaN(Number(key))) // Filter out the reverse mapping keys
.reduce((acc, key) => {
acc[key.toLowerCase()] = QuestVariant[key as keyof typeof QuestVariant];
return acc;
}, {} as Record<string, QuestVariant>);

// Get enum variant for param string or find social variant by quest id
async function getVariantFromParams(params: { variant: string; id: string }) {
const variant = stringToEnumMap[params.variant.toLowerCase()] as QuestVariant;
if (typeof variant === 'undefined') {
const quest = await QuestSocial.findById(params.id);
return quest && questInteractionVariantMap[quest.interaction];
}
return variant;
}

const controller = async (req: Request, res: Response) => {
const { params, account } = req;
const variant = await getVariantFromParams({ variant: params.variant, id: params.id });
if (typeof variant === 'undefined') throw new NotFoundError('Could not determine the quest variant');

const quest = await QuestService.findById(variant, params.id);
if (!quest) throw new NotFoundError('Could not find the quest');

const data = await QuestService.getDataForRequest(variant, req, { quest, account });
const validationResult = await QuestService.getValidationResult(variant, {
quest,
account,
data,
});
if (!validationResult.result) return res.json({ error: validationResult.reason });

const job = await agenda.now(JobType.CreateQuestEntry, {
variant,
questId: quest.id,
sub: account.sub,
data: {
...data,
metadata: validationResult,
},
});

res.json({ jobId: job.attrs._id });
};

export { controller, validation };

This file was deleted.

16 changes: 0 additions & 16 deletions apps/api/src/app/controllers/quests/gitcoin/gitcoin.router.ts

This file was deleted.

This file was deleted.

16 changes: 0 additions & 16 deletions apps/api/src/app/controllers/quests/invite/invite.router.ts

This file was deleted.

Loading

0 comments on commit 1cc113a

Please sign in to comment.