-
Notifications
You must be signed in to change notification settings - Fork 3
Online shop - mappi consumption and computation of seller item expiration. #311
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
adisa39
wants to merge
10
commits into
dev
Choose a base branch
from
online-shop/mappi-consumption
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
356a393
added mappi deduction logics
adisa39 999b37e
added mappi deduction to buyer order checkout logics
adisa39 252f22a
included helper function to extend/reduce seller item expiration date
adisa39 035ca51
modified seller item API endpoint to include mappi consumption
adisa39 1412384
Merge branch 'dev' of https://github.com/map-of-pi/map-of-pi-backend-…
adisa39 a0c470f
added notification for insufficient mappi balance
adisa39 cb1bdb4
handle insufficient mappi balance on order checkout
adisa39 5630921
added custom error management to mappi deductions
adisa39 fed944c
fix new item listing bug
adisa39 6f7cd0a
updated logics to include mappi deductions for deleted seller item
adisa39 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,6 @@ | ||
export class MappiDeductionError extends Error { | ||
constructor(public pi_uid: string, public amount: number, message: string) { | ||
super(message); | ||
this.name = "MappiDeductionError"; | ||
} | ||
} |
This file contains hidden or 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,72 @@ | ||
import logger from "../config/loggingConfig"; | ||
import { ISellerItem } from "../types"; | ||
|
||
export const isExpiredItem = (item: ISellerItem): boolean => { | ||
if (!item || !item.expired_by) return true; | ||
return new Date() > new Date(item.expired_by); | ||
}; | ||
|
||
export const getRemainingWeeks = (existing_item: ISellerItem): number => { | ||
if (!existing_item || !existing_item.expired_by || !existing_item.duration) return 0; | ||
|
||
const now = new Date(); | ||
const expiry = new Date(existing_item.expired_by); | ||
|
||
// Calculate total weeks from duration | ||
const totalWeeks = Math.floor(Number(existing_item.duration)); | ||
|
||
// Calculate weeks left (excluding current week) | ||
const msPerWeek = 7 * 24 * 60 * 60 * 1000; | ||
const msLeft = expiry.getTime() - now.getTime(); | ||
const weeksLeft = Math.floor(msLeft / msPerWeek); | ||
|
||
// Exclude current week | ||
const remainingWeeks = Math.max(weeksLeft - 1, 0); | ||
|
||
// Ensure not more than total duration | ||
return Math.min(remainingWeeks, totalWeeks); | ||
}; | ||
|
||
export const getChangeInWeeks = (existingItem: ISellerItem, itemData: ISellerItem): number => { | ||
const newDuration = Math.max(Number(itemData.duration) || 1, 1); | ||
const existingDuration = Math.max(Number(existingItem.duration) || 1, 1); | ||
|
||
const change = newDuration - existingDuration; | ||
|
||
if (change < 0) { | ||
const remainingWeeks = getRemainingWeeks(existingItem); | ||
if (Math.abs(change) > remainingWeeks) { | ||
logger.warn(`Attempted to reduce duration by ${ Math.abs(change) } weeks, but only ${ remainingWeeks } weeks remain.`); | ||
return 0; // Prevent reducing more than remaining weeks | ||
} | ||
} | ||
|
||
return change; | ||
}; | ||
|
||
export const computeNewExpiryDate = (existingItem: ISellerItem, itemData: ISellerItem): Date => { | ||
const now = new Date(); | ||
const msPerWeek = 7 * 24 * 60 * 60 * 1000; | ||
|
||
if (isExpiredItem(existingItem)) { | ||
// Reset to new duration from now | ||
const newDuration = Math.max(Number(itemData.duration) || 1, 1); | ||
return new Date(now.getTime() + newDuration * msPerWeek); | ||
} | ||
|
||
const expiry = new Date(existingItem.expired_by); | ||
const changeInWeeks = getChangeInWeeks(existingItem, itemData); | ||
|
||
// If duration increased, extend expiry | ||
if (changeInWeeks > 0) { | ||
return new Date(expiry.getTime() + changeInWeeks * msPerWeek); | ||
} | ||
|
||
// If duration decreased, reduce expiry | ||
if (changeInWeeks < 0) { | ||
return new Date(expiry.getTime() + changeInWeeks * msPerWeek); | ||
} | ||
|
||
// No change; | ||
return expiry; | ||
}; |
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍