Skip to content
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

Deprecate Allocations on pickup point functions template. #488

Merged
merged 4 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,11 @@ the [schema](./schema.graphql).

```json
{
"allocations": [
{
"deliveryAddress": {
"countryCode": "CA",
"longitude": 43.70,
"latitude": -79.42
}
}
]
"deliveryAddres": {
"countryCode": "CA",
"longitude": 43.70,
"latitude": -79.42
}
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2940,13 +2940,18 @@ type Input {
"""
A list of allocations.
"""
allocations: [Allocation!]!
allocations: [Allocation!]! @deprecated(reason: "Use `deliveryAddress` instead.")

"""
The cart.
"""
cart: Cart!

"""
The delivery address for pickup point delivery option.
"""
deliveryAddress: MailingAddress!

"""
The result of the fetch target. Refer to network access for Shopify Functions.
"""
Expand Down Expand Up @@ -4303,7 +4308,7 @@ type PurchasingCompany {
"""
Represents how products and variants can be sold and purchased.
"""
type SellingPlan {
type SellingPlan implements HasMetafields {
"""
The description of the selling plan.
"""
Expand All @@ -4314,6 +4319,21 @@ type SellingPlan {
"""
id: ID!

"""
Returns a metafield by namespace and key that belongs to the resource.
"""
metafield(
"""
The key for the metafield.
"""
key: String!

"""
The container the metafield belongs to. If omitted, the app-reserved namespace will be used.
"""
namespace: String
): Metafield

"""
The name of the selling plan. For example, '6 weeks of prepaid granola, delivered weekly'.
"""
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
query FetchInput {
allocations {
deliveryAddress {
countryCode
longitude
latitude
}
deliveryAddress{
countryCode
longitude
latitude
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
export function fetch(input) {
let deliveryAddress = getUniformDeliveryAddress(input.allocations);
if (deliveryAddress) {
let { countryCode, longitude, latitude } = deliveryAddress;
if (longitude && latitude && countryCode === 'CA') {
return {
request: buildExternalApiRequest(latitude, longitude),
};
}
let { countryCode, longitude, latitude } = input.deliveryAddress;
if (longitude && latitude && countryCode === 'CA') {
return {
request: buildExternalApiRequest(latitude, longitude),
};
}
return { request: null };
}
Expand All @@ -28,26 +25,3 @@ function buildExternalApiRequest(latitude, longitude) {
},
};
}

function getUniformDeliveryAddress(allocations) {
if (allocations.length === 0) {
return null;
}

let deliveryAddress = allocations[0].deliveryAddress;

for (let i = 1; i < allocations.length; i++) {
if (!isDeliveryAddressEqual(allocations[i].deliveryAddress, deliveryAddress)) {
console.error("Allocations pointing to different delivery addresses are not supported.");
return null;
}
}

return deliveryAddress;
}

function isDeliveryAddressEqual(address1, address2) {
return address1.countryCode === address2.countryCode &&
address1.longitude === address2.longitude &&
address1.latitude === address2.latitude;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,11 @@ import { fetch } from './fetch';
describe('fetch function', () => {
it('returns a request when country is Canada', () => {
const result = fetch({
allocations: [
{
deliveryAddress: {
countryCode: 'CA',
longitude: 12.3,
latitude: 45.6,
}
}
]
deliveryAddress: {
countryCode: 'CA',
longitude: 12.3,
latitude: 45.6,
}
});
const expected = ({
request: {
Expand All @@ -37,77 +33,13 @@ describe('fetch function', () => {

it('returns no request when country is not Canada', () => {
const result = fetch({
allocations: [
{
deliveryAddress: {
countryCode: 'US',
longitude: 12.3,
latitude: 45.6,
}
}
]
});
const expected = ({ request: null });

expect(result).toEqual(expected);
});

it('returns no request when allocations have different addresses', () => {
const result = fetch({
allocations: [
{
deliveryAddress: {
countryCode: 'CA',
longitude: 12.3,
latitude: 45.6,
}
},
{
deliveryAddress: {
countryCode: 'CA',
longitude: 78.9,
latitude: 10.1,
}
}
]
});
const expected = ({ request: null });

expect(result).toEqual(expected);
});

it('returns a request when allocations have the same address', () => {
const result = fetch({
allocations: [
{
deliveryAddress: {
countryCode: 'CA',
longitude: 12.3,
latitude: 45.6,
}
},
{
deliveryAddress: {
countryCode: 'CA',
longitude: 12.3,
latitude: 45.6,
}
}
]
});
const expected = ({
request: {
body: null,
headers: [
{ name: "Accept", value: "application/json; charset=utf-8" },
],
method: 'GET',
policy: {
readTimeoutMs: 500,
},
url: 'https://cdn.shopify.com/s/files/1/0628/3830/9033/files/pickup-points-external-api-v1.json?v=1712853748&lat=45.6&lon=12.3',
deliveryAddress: {
countryCode: 'US',
longitude: 12.3,
latitude: 45.6,
}
});
const expected = ({ request: null });

expect(result).toEqual(expected);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,11 @@ the [schema](./schema.graphql).

```json
{
"allocations": [
{
"deliveryAddress": {
"countryCode": "CA",
"longitude": 43.70,
"latitude": -79.42
}
}
]
"deliveryAddress": {
"countryCode": "CA",
"longitude": 43.70,
"latitude": -79.42
}
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2940,13 +2940,18 @@ type Input {
"""
A list of allocations.
"""
allocations: [Allocation!]!
allocations: [Allocation!]! @deprecated(reason: "Use `deliveryAddress` instead.")

"""
The cart.
"""
cart: Cart!

"""
The delivery address for pickup point delivery option.
"""
deliveryAddress: MailingAddress!

"""
The result of the fetch target. Refer to network access for Shopify Functions.
"""
Expand Down Expand Up @@ -4303,7 +4308,7 @@ type PurchasingCompany {
"""
Represents how products and variants can be sold and purchased.
"""
type SellingPlan {
type SellingPlan implements HasMetafields {
"""
The description of the selling plan.
"""
Expand All @@ -4314,6 +4319,21 @@ type SellingPlan {
"""
id: ID!

"""
Returns a metafield by namespace and key that belongs to the resource.
"""
metafield(
"""
The key for the metafield.
"""
key: String!

"""
The container the metafield belongs to. If omitted, the app-reserved namespace will be used.
"""
namespace: String
): Metafield

"""
The name of the selling plan. For example, '6 weeks of prepaid granola, delivered weekly'.
"""
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
query Input {
allocations {
deliveryAddress {
countryCode
longitude
latitude
}
deliveryAddress{
countryCode
longitude
latitude
}
}
Loading
Loading