Skip to content

Commit

Permalink
Merge pull request #481 from Shopify/soyed/pup-make-business-hours-op…
Browse files Browse the repository at this point in the history
…tional

Make business hours optional for pickup points templates.
  • Loading branch information
soyed authored Apr 15, 2024
2 parents 4bf1a0a + 3265fd4 commit f16d84e
Show file tree
Hide file tree
Showing 18 changed files with 281 additions and 136 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"discounts/rust/product-discounts/fixed-amount/Cargo.toml",
"discounts/rust/shipping-discounts/default/Cargo.toml",
"discounts/rust/shipping-discounts/fixed-amount/Cargo.toml",
"sample-apps/discounts/extensions/product-discount-rust/Cargo.toml"
"sample-apps/discounts/extensions/product-discount-rust/Cargo.toml",
"order-routing/rust/pickup-point-delivery-option-generators/default/Cargo.toml"
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

This repository contains a function that demonstrates how to generate pickup point delivery options based on an external
API accessible via an HTTP request. To simulate an external API, we have hosted a
[JSON file](https://cdn.shopify.com/s/files/1/0628/3830/9033/files/pickup-points-external-api.json?v=1706549257),
[JSON file](https://cdn.shopify.com/s/files/1/0628/3830/9033/files/pickup-points-external-api-v1.json?v=1712853748),
which contains pickup point information in the following format:

```json
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2692,29 +2692,17 @@ enum DeliveryMethod {
}

"""
The result of a pickup point delivery option generator function fetch command.
The fetch target result. Refer to network access for Shopify Functions.
"""
input FunctionFetchResult {
"""
Request.
HTTP Request.
"""
request: HttpRequest
}

"""
The result of a pickup point delivery option generator function run command. In
API versions 2023-10 and beyond, this type is deprecated in favor of
`FunctionRunResult`.
"""
input FunctionResult {
"""
An ordered list of operations to apply for pickup point delivery option generation.
"""
operations: [Operation!]!
}

"""
The result of a pickup point delivery option generator function run command.
The run target result.
"""
input FunctionRunResult {
"""
Expand Down Expand Up @@ -2960,7 +2948,7 @@ type Input {
cart: Cart!

"""
The HTTP response of the HTTP request from the fetch command.
The result of the fetch target. Refer to network access for Shopify Functions.
"""
fetchResult: HttpResponse @restrictTarget(only: ["purchase.pickup-point-delivery-option-generator.run"])

Expand Down Expand Up @@ -3995,16 +3983,6 @@ type MutationRoot {
result: FunctionFetchResult!
): Void!

"""
Handles the Function result.
"""
handleResult(
"""
The result of the Function.
"""
result: FunctionResult!
): Void! @deprecated(reason: "Use the target-specific field instead.")

"""
Handles the Function result for the purchase.pickup-point-delivery-option-generator.run target.
"""
Expand Down Expand Up @@ -4099,7 +4077,7 @@ input PickupPoint {
The business hours of the pickup point location. Any day that is either not
mentioned or does not have any defined periods will be considered as closed.
"""
businessHours: [BusinessHours!]!
businessHours: [BusinessHours!]

"""
The external id assigned by the provider for the pickup point delivery option.
Expand Down Expand Up @@ -4292,7 +4270,7 @@ The provider for a pickup point.
"""
input Provider {
"""
The provider logo url. The base URL must be `https::/cdn.shopify.com`.
The provider logo url. The base URL must be `https://cdn.shopify.com`.
"""
logoUrl: URL!

Expand Down Expand Up @@ -4499,4 +4477,4 @@ enum WeightUnit {
1 pound equals 16 ounces.
"""
POUNDS
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export function fetch(input) {

function buildExternalApiRequest(latitude, longitude) {
// The latitude and longitude parameters are included in the URL for demonstration purposes only. They do not influence the result.
let url = `https://cdn.shopify.com/s/files/1/0628/3830/9033/files/pickup-points-external-api.json?v=1706549257&lat=${latitude}&lon=${longitude}`;
let url = `https://cdn.shopify.com/s/files/1/0628/3830/9033/files/pickup-points-external-api-v1.json?v=1712853748&lat=${latitude}&lon=${longitude}`

return {
method: 'GET',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe('fetch function', () => {
policy: {
readTimeoutMs: 500,
},
url: 'https://cdn.shopify.com/s/files/1/0628/3830/9033/files/pickup-points-external-api.json?v=1706549257&lat=45.6&lon=12.3',
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',
}
});

Expand Down Expand Up @@ -105,7 +105,7 @@ describe('fetch function', () => {
policy: {
readTimeoutMs: 500,
},
url: 'https://cdn.shopify.com/s/files/1/0628/3830/9033/files/pickup-points-external-api.json?v=1706549257&lat=45.6&lon=12.3',
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',
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,11 @@ function buildAddress(externalApiDeliveryPoint) {
// "Monday: 9:00 AM – 5:00 PM" is transformed to {day: "MONDAY", periods: [{opening_time: "09:00:00", closing_time: "17:00:00"}]}
// "Tuesday: Closed" is transformed to {day: "TUESDAY", periods: []}
function buildBusinessHours(externalApiDeliveryPoint) {
return externalApiDeliveryPoint.openingHours.weekdayText
if(!externalApiDeliveryPoint.openingHours) {
return null;
}

return externalApiDeliveryPoint.openingHours.weekdayText
.map(day => {
let dayParts = day.split(": ");
let dayName = dayParts[0].toUpperCase();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { describe, it, expect } from 'vitest';
import { run } from './run';


describe('run function', () => {
it('returns operations when fetch result is successful', () => {
const result = run({
Expand Down Expand Up @@ -101,4 +102,72 @@ describe('run function', () => {

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

it("returns an add operation with null pickup point business hours when the external API's delivery point has null opening hours", () => {
const result = run({
fetchResult: {
status: 200,
body: JSON.stringify({
deliveryPoints: [
{
location: {
addressComponents: {
country: 'Canada',
countryCode: 'CA',
streetNumber: '123',
route: 'Main St',
locality: 'Toronto',
administrativeAreaLevel1: 'ON',
postalCode: 'M5V 2T6',
},
geometry: {
location: {
lat: 43.70,
lng: -79.42,
},
},
},
openingHours: null,
pointId: '1',
pointName: 'Point 1',
},
],
}),
},
});

const expected = {
operations: [
{
add: {
cost: null,
pickupPoint: {
address: {
address1: '123 Main St',
address2: null,
city: 'Toronto',
country: 'Canada',
countryCode: 'CA',
latitude: 43.70,
longitude: -79.42,
phone: null,
province: 'ON',
provinceCode: null,
zip: 'M5V 2T6',
},
businessHours: null,
provider: {
name: 'Shopify Javascript Demo',
logoUrl: 'https://cdn.shopify.com/s/files/1/0628/3830/9033/files/shopify_icon_146101.png?v=1706120545',
},
externalId: '1',
name: 'Point 1',
},
},
},
],
};

expect(result).toEqual(expected);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

This repository contains a function that demonstrates how to generate pickup point delivery options based on an external
API accessible via an HTTP request. To simulate an external API, we have hosted a
[JSON file](https://cdn.shopify.com/s/files/1/0628/3830/9033/files/pickup-points-external-api.json?v=1706549257),
[JSON file](https://cdn.shopify.com/s/files/1/0628/3830/9033/files/pickup-points-external-api-v1.json?v=1712853748),
which contains pickup point information in the following format:

```json
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2692,29 +2692,17 @@ enum DeliveryMethod {
}

"""
The result of a pickup point delivery option generator function fetch command.
The fetch target result. Refer to network access for Shopify Functions.
"""
input FunctionFetchResult {
"""
Request.
HTTP Request.
"""
request: HttpRequest
}

"""
The result of a pickup point delivery option generator function run command. In
API versions 2023-10 and beyond, this type is deprecated in favor of
`FunctionRunResult`.
"""
input FunctionResult {
"""
An ordered list of operations to apply for pickup point delivery option generation.
"""
operations: [Operation!]!
}

"""
The result of a pickup point delivery option generator function run command.
The run target result.
"""
input FunctionRunResult {
"""
Expand Down Expand Up @@ -2960,7 +2948,7 @@ type Input {
cart: Cart!

"""
The HTTP response of the HTTP request from the fetch command.
The result of the fetch target. Refer to network access for Shopify Functions.
"""
fetchResult: HttpResponse @restrictTarget(only: ["purchase.pickup-point-delivery-option-generator.run"])

Expand Down Expand Up @@ -3995,16 +3983,6 @@ type MutationRoot {
result: FunctionFetchResult!
): Void!

"""
Handles the Function result.
"""
handleResult(
"""
The result of the Function.
"""
result: FunctionResult!
): Void! @deprecated(reason: "Use the target-specific field instead.")

"""
Handles the Function result for the purchase.pickup-point-delivery-option-generator.run target.
"""
Expand Down Expand Up @@ -4099,7 +4077,7 @@ input PickupPoint {
The business hours of the pickup point location. Any day that is either not
mentioned or does not have any defined periods will be considered as closed.
"""
businessHours: [BusinessHours!]!
businessHours: [BusinessHours!]

"""
The external id assigned by the provider for the pickup point delivery option.
Expand Down Expand Up @@ -4292,7 +4270,7 @@ The provider for a pickup point.
"""
input Provider {
"""
The provider logo url. The base URL must be `https::/cdn.shopify.com`.
The provider logo url. The base URL must be `https://cdn.shopify.com`.
"""
logoUrl: URL!

Expand Down Expand Up @@ -4499,4 +4477,4 @@ enum WeightUnit {
1 pound equals 16 ounces.
"""
POUNDS
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ fn fetch(input: fetch::input::ResponseData) -> Result<fetch::output::FunctionFet
fn build_external_api_request(latitude: &f64, longitude: &f64) -> fetch::output::HttpRequest {
// The latitude and longitude parameters are included in the URL for demonstration purposes only. They do not influence the result.
let url = format!(
"https://cdn.shopify.com/s/files/1/0628/3830/9033/files/pickup-points-external-api.json?v=1706549257&lat={}&lon={}",
"https://cdn.shopify.com/s/files/1/0628/3830/9033/files/pickup-points-external-api-v1.json?v=1712853748&lat={}&lon={}",
latitude, longitude
);

Expand Down Expand Up @@ -89,7 +89,7 @@ mod tests {
let expected = FunctionFetchResult {
request: Some(HttpRequest {
method: HttpRequestMethod::GET,
url: "https://cdn.shopify.com/s/files/1/0628/3830/9033/files/pickup-points-external-api.json?v=1706549257&lat=-79.42&lon=43.7".to_string(),
url: "https://cdn.shopify.com/s/files/1/0628/3830/9033/files/pickup-points-external-api-v1.json?v=1712853748&lat=-79.42&lon=43.7".to_string(),

headers: vec![
HttpRequestHeader {
Expand Down Expand Up @@ -192,7 +192,7 @@ mod tests {
let expected = FunctionFetchResult {
request: Some(HttpRequest {
method: HttpRequestMethod::GET,
url: "https://cdn.shopify.com/s/files/1/0628/3830/9033/files/pickup-points-external-api.json?v=1706549257&lat=-79.42&lon=43.7".to_string(),
url: "https://cdn.shopify.com/s/files/1/0628/3830/9033/files/pickup-points-external-api-v1.json?v=1712853748&lat=-79.42&lon=43.7".to_string(),

headers: vec![
HttpRequestHeader {
Expand Down
Loading

0 comments on commit f16d84e

Please sign in to comment.