Skip to content

Commit

Permalink
Changed getAllStores to use vinmonopolet api. Removed stream api.
Browse files Browse the repository at this point in the history
  • Loading branch information
LarsEllefsen committed Jun 16, 2024
1 parent 59857ff commit 7409a98
Show file tree
Hide file tree
Showing 10 changed files with 213 additions and 287 deletions.
6 changes: 0 additions & 6 deletions src/csvUrls.ts

This file was deleted.

2 changes: 0 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,3 @@ export { default as getAllStores, searchStores } from "./retrievers/getStores";
export { default as searchProducts } from "./retrievers/searchProducts";
export { default as getStore } from "./retrievers/getStore";
export { default as getProductReleases } from "./retrievers/getProductReleases";

export { default as stream } from "./stream";
188 changes: 36 additions & 152 deletions src/models/Store.ts
Original file line number Diff line number Diff line change
@@ -1,52 +1,9 @@
import getStore from "../retrievers/getStore";

interface IOpeningHours {
opens: { hour: number; minute: number };
closes: { hour: number; minute: number };
export interface IOpeningHours {
weekDay: string;
opens?: { hour: number; minute: number };
closes?: { hour: number; minute: number };
}

const getOpeningHours = (
weekday: string,
store: { [property: string]: any }
): IOpeningHours | null => {
try {
let weekDayOpening =
store["Apn_" + weekday.toLowerCase().replace("ø", "o")];
weekDayOpening = weekDayOpening ?? store.openingTimes;
if (Array.isArray(weekDayOpening)) {
const weekDayEntry = weekDayOpening.find(
(entry) => entry.weekDay.toLowerCase() === weekday.toLowerCase()
);
if (weekDayEntry.closed || !weekDayEntry) return null;
return {
opens: weekDayEntry?.openingTime,
closes: weekDayEntry?.closingTime,
} as IOpeningHours;
}

if (weekDayOpening && weekDayOpening != "Stengt") {
weekDayOpening = weekDayOpening as string;
let [opening, closing]: string[] = weekDayOpening.split("-");
opening = opening.trim();
closing = closing.trim();
return {
opens: {
hour: Number(opening?.slice(0, 3)),
minute: Number(opening?.slice(2, opening.length)),
},
closes: {
hour: Number(closing?.slice(0, 3)),
minute: Number(closing?.slice(2, opening.length)),
},
} as IOpeningHours;
}

return null;
} catch (e) {
return null;
}
};

export class BaseStore {
/**
* Unique ID for the store.
Expand All @@ -63,46 +20,31 @@ export class BaseStore {
/**
* The zip code of the store.
*/
streetZip: string;
zip?: string;
/**
* The city the store is located in.
*/
streetCity: string;
/**
* The postal address. Usually just the same as streetAddress.
*/
postalAddress: string;
/**
* The zip code of the stores postal address. Usually just the same as streetZip.
*/
postalZip: string;
/**
* The postal city of the store. Usually just the same as the streetCity property.
*/
postalCity: string;
/**
* The phone number for the store.
*/
phoneNumber: string;
city?: string;
/**
* GPS coordinates of the store given as a [lat, lon] array.
*/
gpsCoordinates: [number, number];

constructor(store) {
this.name = store?.Butikknavn ?? store?.displayName;
this.streetAddress = store.Gateadresse ?? store.address.line1;
this.streetZip = store?.Gate_postnummer ?? store.address.postalCode;
this.streetCity = store?.Gate_poststed ?? store.address.town;
this.postalAddress = store?.Postadresse ?? store.address.line2;
this.postalZip = store?.Post_postnummer ?? store.address.postalCode;
this.postalCity = store?.Post_poststed ?? store.address.town;
this.phoneNumber = store?.Telefonnummer ?? store.address.phone;
this.gpsCoordinates = [
store?.GPS_breddegrad ?? store?.geoPoint.latitude,
store?.GPS_lengdegrad ?? store?.geoPoint.longitude,
];
this.storeNumber = store?.Butikknummer ?? store?.name;
constructor(
storeNumber: string,
name: string,
streetAddress: string,
zip: string | undefined,
city: string | undefined,
latitude: number,
longitude: number
) {
this.storeNumber = storeNumber;
this.name = name;
this.streetAddress = streetAddress;
this.zip = zip;
this.city = city;
this.gpsCoordinates = [latitude, longitude];
}

/**
Expand All @@ -124,80 +66,22 @@ class PopulatedStore extends BaseStore {
* The category of the store. The category ranges from 1 to 7, where 1 is the lowest possible product selection and 7 is the best possible product selection.
*/
category: string;
/**
* The current week. Usually undefined unless using getAllStores.
*/
weekNumber: number | undefined;
/**
* An oject representing the opening and closing times of the store on monday this week. Is null if the store is not open that day.
*/
openingHoursMonday: IOpeningHours | null;
/**
* An oject representing the opening and closing times of the store on tuesday this week. Is null if the store is not open that day.
*/
openingHoursTuesday: IOpeningHours | null;
/**
* An oject representing the opening and closing times of the store on wednesday this week. Is null if the store is not open that day.
*/
openingHoursWednesday: IOpeningHours | null;
/**
* An oject representing the opening and closing times of the store on thursday this week. Is null if the store is not open that day.
*/
openingHoursThursday: IOpeningHours | null;
/**
* An oject representing the opening and closing times of the store on friday this week. Is null if the store is not open that day.
*/
openingHoursFriday: IOpeningHours | null;
/**
* An oject representing the opening and closing times of the store on saturday this week. Is null if the store is not open that day.
*/
openingHoursSaturday: IOpeningHours | null;
/**
* The next weeks number.
*/
weekNumberNext: number;
/**
* An oject representing the opening and closing times of the store on monday next week. Is null if the store is not open that day.
*/
openingHoursNextMonday: IOpeningHours | null;
/**
* An oject representing the opening and closing times of the store on tuesday next week. Is null if the store is not open that day.
*/
openingHoursNextTuesday: IOpeningHours | null;
/**
* An oject representing the opening and closing times of the store on wendesday next week. Is null if the store is not open that day.
*/
openingHoursNextWednesday: IOpeningHours | null;
/**
* An oject representing the opening and closing times of the store on thursday next week. Is null if the store is not open that day.
*/
openingHoursNextThursday: IOpeningHours | null;
/**
* An oject representing the opening and closing times of the store on friday next week. Is null if the store is not open that day.
*/
openingHoursNextFriday: IOpeningHours | null;
/**
* An oject representing the opening and closing times of the store on saturday next week. Is null if the store is not open that day.
*/
openingHoursNextSaturday: IOpeningHours | null;
openingHours: IOpeningHours[];

constructor(store) {
super(store);
this.category = store?.Kategori ?? store?.assortment;
this.weekNumber = store?.Ukenummer;
this.openingHoursMonday = getOpeningHours("mandag", store);
this.openingHoursTuesday = getOpeningHours("tirsdag", store);
this.openingHoursWednesday = getOpeningHours("onsdag", store);
this.openingHoursThursday = getOpeningHours("torsdag", store);
this.openingHoursFriday = getOpeningHours("fredag", store);
this.openingHoursSaturday = getOpeningHours("lørdag", store);
this.weekNumberNext = store?.Ukenummer_neste;
this.openingHoursNextMonday = store?.Apn_neste_mandag;
this.openingHoursNextTuesday = store?.Apn_neste_tirsdag;
this.openingHoursNextWednesday = store?.Apn_neste_onsdag;
this.openingHoursNextThursday = store?.Apn_neste_torsdag;
this.openingHoursNextFriday = store?.Apn_neste_fredag;
this.openingHoursNextSaturday = store?.Apn_neste_lordag;
constructor(
storeNumber: string,
name: string,
streetAddress: string,
zip: string,
city: string,
latitude: number,
longitude: number,
category: string,
openingHours: IOpeningHours[]
) {
super(storeNumber, name, streetAddress, zip, city, latitude, longitude);
this.category = category;
this.openingHours = openingHours;
}

populate(): Promise<PopulatedStore> {
Expand Down
2 changes: 1 addition & 1 deletion src/retrievers/getFacets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Facet from "../models/Facet";

function getFacets(): Promise<Array<Facet>> {
return request
.get("/vmpws/v2/vmp/search", {
.get<{ [key: string]: any }>("/vmpws/v2/vmp/search", {
baseUrl: "https://www.vinmonopolet.no",
query: {
fields: "FULL",
Expand Down
2 changes: 1 addition & 1 deletion src/retrievers/getProducts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ function getProducts(
const query = getQuery(opts);

const getter = request.get;
const req = getter("/products/search", { query });
const req = getter<{ [key: string]: any }>("/products/search", { query });

return req.then((res) => ({
products: (res.products || []).map((i) => new Product(i)),
Expand Down
75 changes: 69 additions & 6 deletions src/retrievers/getStore.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,76 @@
import PopulatedStore from "../models/Store";
import PopulatedStore, { IOpeningHours } from "../models/Store";
import request from "../util/request";

interface IOpeningTime {
closingTime: {
formattedHour: string;
hour: number;
minute: number;
};
openingTime: {
formattedHour: string;
hour: number;
minute: number;
};
weekDay: string;
closed: false;
formattedDate: string;
readableValue: string;
}

interface IGetStoreDTO {
address: {
defaultAddress: boolean;
formattedAddress: string;
line1: string;
line2: string;
phone: string;
postalCode: string;
town: string;
};
assortment: string;
clickAndCollect: boolean;
displayName: string;
geoPoint: {
latitude: number;
longitude: number;
};
name: string;
openingTimes: IOpeningTime[];
}

const getStore = async (store_id: string): Promise<PopulatedStore> => {
const res = await request.get(`/vmpws/v2/vmp/stores/${store_id}`, {
baseUrl: "https://www.vinmonopolet.no",
query: { fields: "FULL" },
});
const res = await request.get<IGetStoreDTO>(
`/vmpws/v2/vmp/stores/${store_id}`,
{
baseUrl: "https://www.vinmonopolet.no",
query: { fields: "FULL" },
}
);

return new PopulatedStore(res);
return toPopulatedStore(res);
};

function toPopulatedStore(storeDTO: IGetStoreDTO): PopulatedStore {
return new PopulatedStore(
storeDTO.name,
storeDTO.displayName,
storeDTO.address.line1,
storeDTO.address.postalCode,
storeDTO.address.town,
storeDTO.geoPoint.latitude,
storeDTO.geoPoint.longitude,
storeDTO.assortment,
storeDTO.openingTimes.map(toOpeningHours)
);
}

function toOpeningHours(openingTimeDTO: IOpeningTime): IOpeningHours {
return {
closes: openingTimeDTO.closingTime,
opens: openingTimeDTO.openingTime,
weekDay: openingTimeDTO.weekDay,
};
}

export default getStore;
Loading

0 comments on commit 7409a98

Please sign in to comment.