-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#23 | add, update, cancel subscription
- Loading branch information
Showing
20 changed files
with
343 additions
and
67 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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 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 |
---|---|---|
@@ -1,7 +1,90 @@ | ||
import Apollo from 'apollo-server-express'; | ||
|
||
import logger from '~/utils/logger'; | ||
import { | ||
deleteUserPlanById, | ||
getUserPlanById, | ||
getUserPlanByUserId, | ||
insertUserPlan, | ||
updateUserPlanById, | ||
} from '~/repository/user_plans.repository'; | ||
import { cancelSubcription, createNewSubcription, updateSubcription } from '~/services/stripe/subcription.service'; | ||
import { findProductAndPriceByType } from '~/repository/products.repository'; | ||
import { findUser } from '~/repository/user.repository'; | ||
|
||
export async function getUserPlan(userId) { | ||
const { ApolloError } = Apollo; | ||
export function getUserPlan(userId) { | ||
return getUserPlanByUserId(userId); | ||
} | ||
|
||
export async function createUserPlan(userId, paymentMethodToken, planName, billingType) { | ||
try { | ||
const user = await findUser({ id: userId }); | ||
if (!user) { | ||
throw new ApolloError('Can not find any user'); | ||
} | ||
|
||
const product = await findProductAndPriceByType(planName, billingType); | ||
if (!product) { | ||
throw new ApolloError('Can not find any plan'); | ||
} | ||
|
||
const subscriptionId = await createNewSubcription(paymentMethodToken, user.email, user.name, product.price_stripe_id); | ||
const dataUserPlan = { | ||
user_id: userId, | ||
product_id: product.id, | ||
price_id: product.price_id, | ||
subcription_id: subscriptionId, | ||
}; | ||
await insertUserPlan(dataUserPlan); | ||
|
||
return true; | ||
} catch (error) { | ||
logger.error(error); | ||
throw new ApolloError('Something went wrong!'); | ||
} | ||
} | ||
|
||
export async function updateUserPlan(usePlanId, planName, billingType) { | ||
try { | ||
const userPlan = await getUserPlanById(usePlanId); | ||
if (!userPlan) { | ||
throw new ApolloError('Can not find any user plan'); | ||
} | ||
|
||
const product = await findProductAndPriceByType(planName, billingType); | ||
if (!product) { | ||
throw new ApolloError('Can not find any plan'); | ||
} | ||
|
||
await updateSubcription(userPlan.subcription_id, product.price_stripe_id); | ||
|
||
const dataUserPlan = { | ||
product_id: product.id, | ||
price_id: product.price_id, | ||
}; | ||
await updateUserPlanById(usePlanId, dataUserPlan); | ||
|
||
return true; | ||
} catch (error) { | ||
logger.error(error); | ||
throw new ApolloError('Something went wrong!'); | ||
} | ||
} | ||
|
||
export async function deleteUserPlan(id) { | ||
try { | ||
const userPlan = await getUserPlanById(id); | ||
if (!userPlan) { | ||
throw new ApolloError('Can not find any user plan'); | ||
} | ||
|
||
await cancelSubcription(userPlan.subcription_id); | ||
await deleteUserPlanById(id); | ||
|
||
return true; | ||
} catch (error) { | ||
logger.error(error); | ||
throw new ApolloError('Something went wrong!'); | ||
} | ||
} |
This file contains 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 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 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 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.