Skip to content
This repository has been archived by the owner on Jan 13, 2020. It is now read-only.

Fix import #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
7 changes: 3 additions & 4 deletions src/controllers/productController.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import { getRepository } from "typeorm";
import { Product } from '../entity/Product';
import { HiiperService } from "../services/HiiperService"
import {getMongoRepository} from "typeorm";
import { getMongoRepository } from "typeorm";

class ProductController {

static saveProducts = async () => {
const productRepository = getMongoRepository(Product);
const hiiperService = new HiiperService();
hiiperService.RequestProductsAsync().then(function (value) {
//console.log(value[0]);
productRepository.insertMany(value[0]);
productRepository.insertMany(value);
});
};
};
Expand Down
3 changes: 0 additions & 3 deletions src/entity/Product.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ import {
Entity,
PrimaryGeneratedColumn,
Column,
Unique,
CreateDateColumn,
UpdateDateColumn
} from "typeorm";
import { Length, IsNotEmpty } from "class-validator";

Expand Down
2 changes: 1 addition & 1 deletion src/routes/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const routes = Router();

routes.use("/auth", auth);
routes.use("/user", user);
routes.use("/product", product);
routes.use("/import", product);


export default routes;
32 changes: 14 additions & 18 deletions src/services/HiiperService.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const req = require("request");
import { getRepository } from "typeorm";
import { Product } from "../entity/Product";
let cookie : object;

export class HiiperService {
private email: string;
Expand Down Expand Up @@ -33,27 +34,30 @@ export class HiiperService {
},
function (error, response): void {
if (!error && response.statusCode == 200) {
console.log("Succesfully setup hiiper session")
resolve(cookie)
}
else {
console.log(error);
console.log(response.statusCode);
reject("Error setting up Hiiper Session")
}
}
);
});
}

private async HiiperRequestAsync(hiperUri: string) {
const cookie: object = await this.SetupSessionAsync();
private async HiiperRequestAsync(hiiperUri: string) {
if(!cookie){
cookie = await this.SetupSessionAsync();
}
return new Promise((resolve, reject): void => {
req.get(hiperUri, { jar: cookie, headers: this.headers }, function (error, response, body): void {
req.get(hiiperUri, { jar: cookie, headers: this.headers }, function (error, response, body): void {
if (!error && response.statusCode == 200) {
const result: any = JSON.parse(body)
const result: any = JSON.parse(body);
resolve(result.items);
}
else {
console.log(error);
console.log(response);
reject("Error executing HiiperRequestAsync")
}
});
Expand All @@ -65,26 +69,18 @@ export class HiiperService {
let pageSize: number = 1;
let productUri: string = `https://api.hiiper.nl/api/v1/client/clickout/deals?deals_type=general&items_in_row=11&r_limit=0&rarity=0&rows_per_page=5&page=${pageSize}`
let retrievedProducts: any = await this.HiiperRequestAsync(productUri);
products.push(retrievedProducts);

// .push.apply makes sure the structure will be [object,object] instead of [[object,object], [object,object]]
products.push.apply(products, retrievedProducts);
pageSize++;
// Increase page while there is data
while (retrievedProducts.length > 0) {
productUri = `https://api.hiiper.nl/api/v1/client/clickout/deals?deals_type=general&items_in_row=11&r_limit=0&rarity=0&rows_per_page=5&page=${pageSize}`
retrievedProducts = await this.HiiperRequestAsync(productUri);
console.log(`Succesfully retrieved data from ${productUri}`);
if(retrievedProducts.length > 0) {
products.push(retrievedProducts[0]);
}
products.push.apply(products, retrievedProducts);
pageSize++;
};
return products;
};
};

// const productRepository = getRepository(Product);

// const hiiperService = new HiiperService();
// hiiperService.RequestProductsAsync().then(function (value) {
// //console.log(value);
// productRepository.save(value);
// });