All files / iceCreamProviders IceCreamPnPJsProvider.ts

90% Statements 18/20
100% Branches 0/0
80% Functions 8/10
87.5% Lines 14/16

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46        2x         4x     2x   1x               1x   1x 3x 3x     1x         2x   1x 1x       1x       2x
import { IceCream } from "./IceCream";
import { IIceCreamProvider } from "./IIceCreamProvider";
import { SPRest, SearchQuery, SearchResult, SearchResults } from "@pnp/sp";
 
export class IceCreamPnPJsProvider implements IIceCreamProvider {
 
    private readonly sp: SPRest;
 
    constructor(sp: SPRest) {
        this.sp = sp;
    }
 
    public getAll(): Promise<IceCream[]> {
 
        return new Promise<IceCream[]>(async (resolve, reject) => {
 
            const query: SearchQuery = {
                RowLimit: 10,
                SelectProperties: ["UniqueId", "Title", "PriceOWSNMBR"],
                Querytext: 'path:https://spfxjest.sharepoint.com/sites/jest/Lists/IceCreamFlavours AND contenttypeid:0x01*'
            } as SearchQuery;
 
            this.sp.search(query).then((searchResults: SearchResults) => {
 
                const result = [];
                for (const item of searchResults.PrimarySearchResults) {
                    result.push({ UniqueId: item.UniqueId, Title: item.Title, Price: Math.round(item["PriceOWSNMBR"] * 100) / 100 });
                }
 
                resolve(result);
            }).catch(error => reject(error));
        });
    }
 
    public buy(uniqueid: string, quantity: number): Promise<any> {
 
        return new Promise<any>((resolve, reject) => {
            this.sp.web.lists.getByTitle('Ice Cream Orders').items.add({
                "Title": uniqueid,
                "Quantity": quantity
            })
                .then(result => resolve())
                .catch(error => reject(error));
        });
    }
}