Implement Referral System with Schema, Controllers, Routes, and Logging #969
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Overview
This PR introduces the
Referral
schema, controller, and routes to handle referral functionalities in the application. It includes logging for important actions and a utility function to generate unique referral codes.Schema
The
Referral
schema defines the necessary fields for tracking referrals:pending
,completed
,expired
).New Endpoints and Controllers
POST /api/referrals
:createReferral
referrerId
in the request body and returns the generatedreferralCode
along with a success message.referrerId
and the generated referral code.GET /api/referrals/:code/status
:checkReferralStatus
pending
,completed
). The referral code is passed as a parameter in the URL.referralCode
in the database.POST /api/referrals/complete
:completeReferral
referralCode
andrefereeId
in the request body. It ensures that the referral code is valid and hasn't already been used.referralCode
.completed
and links therefereeId
.GET /api/referrals/expired/:code
:checkReferralExpiry
expiryDate
. It returns the status (expired
orvalid
) along with an appropriate message.referralCode
and compares theexpiryDate
with the current date.GET /api/referrals/:referrerId
:getReferralsByReferrer
referrerId
. It lists the generated referral codes and their current status.referrerId
matches the provided value.GET /api/referrals/all
:getAllReferrals
POST /api/referrals/log
:logReferralAction
POST /api/referrals/validate
:validateReferralCode
Utility Functions
generateUniqueCode
: A utility function to generate a random alphanumeric referral code. This function is used in thecreateReferral
controller to ensure that each referral code is unique and not repetitive.Logger Integration
Each critical action, such as creating a referral, completing a referral, or checking the status, is logged with relevant data. This helps with tracking the referral process, debugging, and ensuring that referral activities are auditable. The logging includes details like the referral code, the user involved, and the action taken.
Summary of Endpoints