-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feat/myaccountvtex' of https://github.com/deco-cx/apps …
…into feat/myaccountvtex
- Loading branch information
Showing
8 changed files
with
372 additions
and
50 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { AppContext } from "../../mod.ts"; | ||
import { parseCookie } from "../../utils/vtexId.ts"; | ||
|
||
interface NewsletterInput { | ||
email: string; | ||
isNewsletterOptIn: boolean; | ||
} | ||
|
||
const newsletterProfile = async ( | ||
props: NewsletterInput, | ||
req: Request, | ||
ctx: AppContext, | ||
): Promise<boolean | null> => { | ||
const { io } = ctx; | ||
const { cookie } = parseCookie(req.headers, ctx.account); | ||
|
||
if (!props?.email) { | ||
console.error("User profile not found or email is missing:", props.email); | ||
return null; | ||
} | ||
|
||
const mutation = ` | ||
mutation SubscribeNewsletter($email: String!, $isNewsletterOptIn: Boolean!) { | ||
subscribeNewsletter(email: $email, isNewsletterOptIn: $isNewsletterOptIn) | ||
@context(provider: "[email protected]") | ||
} | ||
`; | ||
|
||
const variables = { | ||
email: props.email, | ||
isNewsletterOptIn: props.isNewsletterOptIn, | ||
}; | ||
|
||
try { | ||
await io.query<{ subscribeNewsletter: boolean }, unknown>( | ||
{ | ||
query: mutation, | ||
operationName: "SubscribeNewsletter", | ||
variables, | ||
}, | ||
{ | ||
headers: { | ||
cookie, | ||
}, | ||
}, | ||
); | ||
|
||
const result = await ctx.invoke("vtex/loaders/user.ts"); | ||
const newsletterField = result?.customFields?.find((field) => | ||
field.key === "isNewsletterOptIn" | ||
); | ||
|
||
return newsletterField?.value === "true"; | ||
} catch (error) { | ||
console.error("Error subscribing to newsletter:", error); | ||
return null; | ||
} | ||
}; | ||
|
||
export default newsletterProfile; |
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 |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import { AppContext } from "../../mod.ts"; | ||
import { parseCookie } from "../../utils/vtexId.ts"; | ||
import { Person } from "../../../commerce/types.ts"; | ||
import type { User } from "../../loaders/user.ts"; | ||
|
||
export interface UserMutation { | ||
firstName?: string; | ||
lastName?: string; | ||
email?: string; | ||
homePhone?: string | null; | ||
gender?: string | null; | ||
birthDate?: string | null; | ||
corporateName?: string | null; | ||
tradeName?: string | null; | ||
businessPhone?: string | null; | ||
isCorporate?: boolean; | ||
} | ||
|
||
const updateProfile = async ( | ||
props: UserMutation, | ||
req: Request, | ||
ctx: AppContext, | ||
): Promise<Person | null> => { | ||
const { io } = ctx; | ||
const { cookie } = parseCookie(req.headers, ctx.account); | ||
|
||
if (!props?.email) { | ||
console.error("User profile not found or email is missing:", props.email); | ||
return null; | ||
} | ||
const mutation = ` | ||
mutation UpdateProfile($input: ProfileInput!) { | ||
updateProfile(fields: $input) @context(provider: "vtex.store-graphql") { | ||
cacheId | ||
firstName | ||
lastName | ||
birthDate | ||
gender | ||
homePhone | ||
businessPhone | ||
document | ||
tradeName | ||
corporateName | ||
corporateDocument | ||
stateRegistration | ||
isCorporate | ||
} | ||
} | ||
`; | ||
|
||
try { | ||
const { updateProfile: updatedUser } = await io.query< | ||
{ updateProfile: User }, | ||
{ input: UserMutation } | ||
>( | ||
{ | ||
query: mutation, | ||
operationName: "UpdateProfile", | ||
variables: { | ||
input: { | ||
...props, | ||
email: props.email, | ||
}, | ||
}, | ||
}, | ||
{ headers: { cookie } }, | ||
); | ||
|
||
return { | ||
"@id": updatedUser?.userId ?? updatedUser.id, | ||
email: updatedUser.email, | ||
givenName: updatedUser?.firstName, | ||
familyName: updatedUser?.lastName, | ||
taxID: updatedUser?.document?.replace(/[^\d]/g, ""), | ||
gender: updatedUser?.gender === "female" | ||
? "https://schema.org/Female" | ||
: "https://schema.org/Male", | ||
telephone: updatedUser?.homePhone, | ||
birthDate: updatedUser?.birthDate, | ||
corporateName: updatedUser?.tradeName, | ||
corporateDocument: updatedUser?.corporateDocument, | ||
businessPhone: updatedUser?.businessPhone, | ||
isCorporate: updatedUser?.isCorporate, | ||
customFields: updatedUser?.customFields, | ||
}; | ||
} catch (error) { | ||
console.error("Error updating user profile:", error); | ||
return null; | ||
} | ||
}; | ||
|
||
export default updateProfile; |
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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { RequestURLParam } from "../../../website/functions/requestToParam.ts"; | ||
import { AppContext } from "../../mod.ts"; | ||
import { Order } from "../../utils/types.ts"; | ||
import { parseCookie } from "../../utils/vtexId.ts"; | ||
|
||
export interface Props { | ||
slug: RequestURLParam; | ||
} | ||
|
||
export default async function loader( | ||
props: Props, | ||
req: Request, | ||
ctx: AppContext, | ||
): Promise<Order | null> { | ||
const { vcsDeprecated } = ctx; | ||
const { cookie } = parseCookie(req.headers, ctx.account); | ||
|
||
const { slug } = props; | ||
|
||
const response = await vcsDeprecated["GET /api/oms/user/orders/:orderId"]( | ||
{ orderId: slug }, | ||
{ | ||
headers: { | ||
cookie, | ||
}, | ||
}, | ||
); | ||
|
||
if (response.ok) { | ||
const order = await response.json(); | ||
return order; | ||
} | ||
|
||
return null; | ||
} |
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.