| import { |
| Offer, |
| OfferFeed, |
| OfferSource, |
| ActionType, |
| OfferMode, |
| OfferCategory |
| } from "./generated/offer"; // Path to your generated types |
| import { Money } from "./generated/money"; |
| import { DayOfWeek } from "./generated/dayofweek"; |
| import { TimeOfDay } from "./generated/timeofday"; |
| import { Timestamp } from "./generated/google/protobuf/timestamp"; |
| import { Duration } from "./generated/google/protobuf/duration"; |
| |
| // 200 MB Limit |
| const MAX_BYTES_DATA_FILE = 200 * 1024 * 1024; |
| |
| function generateOfferFeed() { |
| // Build the Offer object |
| const offer: Offer = { |
| offer_id: "offer-1", |
| entity_ids: ["dining-1"], |
| offer_source: OfferSource.OFFER_SOURCE_AGGREGATOR, |
| action_type: ActionType.ACTION_TYPE_DINING, |
| offer_modes: [ |
| OfferMode.OFFER_MODE_WALK_IN, |
| OfferMode.OFFER_MODE_FREE_RESERVATION, |
| ], |
| offer_category: OfferCategory.OFFER_CATEGORY_BASE_OFFER, |
| offer_details: { |
| offer_display_text: "₹100 off on your order", |
| // Set 'oneof' field: discountValue |
| discount_value: { |
| currency_code: "INR", |
| units: 100, |
| }, |
| }, |
| offer_restrictions: { |
| combinable_with_other_offers: true, |
| combinable_offer_categories: [ |
| OfferCategory.OFFER_CATEGORY_ADD_ON_PAYMENT_OFFER, |
| OfferCategory.OFFER_CATEGORY_ADD_ON_COUPON_OFFER, |
| ], |
| }, |
| terms: { |
| restricted_to_certain_users: false, |
| terms_and_conditions: "Valid on all menu items.", |
| }, |
| validity_periods: [ |
| { |
| valid_period: { |
| valid_from_time: new Date(1687062000000), |
| valid_through_time: new Date(1956556800000), |
| }, |
| time_of_day: [ |
| // Monday - Thursday Window |
| { |
| time_windows: { |
| open_time: { hours: 13, minutes: 0, seconds: 0, nanos: 0 }, |
| close_time: { hours: 23, minutes: 0, seconds: 0, nanos: 0 }, |
| }, |
| day_of_week: [ |
| DayOfWeek.MONDAY, |
| DayOfWeek.TUESDAY, |
| DayOfWeek.WEDNESDAY, |
| DayOfWeek.THURSDAY, |
| ], |
| }, |
| // Friday - Sunday Window |
| { |
| time_windows: { |
| open_time: { hours: 13, minutes: 0, seconds: 0, nanos: 0 }, |
| close_time: { hours: 23, minutes: 59, seconds: 59, nanos: 0 }, |
| }, |
| day_of_week: [ |
| DayOfWeek.FRIDAY, |
| DayOfWeek.SATURDAY, |
| DayOfWeek.SUNDAY, |
| ], |
| }, |
| ], |
| }, |
| ], |
| offer_url: "https://www.example-restaurant.com/offer/base_offer_1", |
| image_url: "https://www.example-restaurant.com/images/offer_base.jpg", |
| }; |
| |
| // 2. Initialize the Feed |
| const feed: OfferFeed = { data: [] }; |
| |
| // 3. Size checking |
| // encode().finish() returns a Uint8Array (the binary representation) |
| const offerSize = new Blob([JSON.stringify(offer)]).size; |
| const currentFeedSize = new Blob([JSON.stringify(feed)]).size; |
| |
| if (currentFeedSize + offerSize < MAX_BYTES_DATA_FILE) { |
| if (feed.data == undefined) { |
| feed.data = [offer]; |
| } else { |
| feed.data.push(offer); |
| } |
| } else { |
| // Logic for file rotation goes here |
| console.log("Feed size limit reached. Rotate file."); |
| } |
| |
| // 4. Serialize to JSON |
| // In JS/TS, we often just use JSON.stringify for the plain object |
| // if you used proto3 and want to ensure proper enum/timestamp formatting, |
| // use the library's toJSON method. |
| const jsonOutput = JSON.stringify(feed); |
| console.log(jsonOutput); |
| } |
| |
| generateOfferFeed(); |