// tslint:disable:no-console
import * as mongoose from 'mongoose';

import { chevre } from '../../../lib/index';

// const project = { id: String(process.env.PROJECT_ID) };

mongoose.Model.on('index', (...args) => {
    console.error('******** index event emitted. ********\n', args);
});

async function main() {
    await mongoose.connect(<string>process.env.MONGOLAB_URI, { autoIndex: false });

    const actionRepo = await chevre.repository.Action.createInstance(mongoose.connection);

    const docs = await actionRepo.searchByPurpose(
        {
            purpose: { id: '66ce6fe0f254c2172e578c9a', typeOf: chevre.factory.transactionType.PlaceOrder },
            // typeOf: chevre.factory.actionType.AuthorizeAction,
            object: {
                // typeOf: { $eq: 'AggregateOffer' }
                // paymentMethodId: { $eq: '137280454658483' }
            },
            actionStatus: { $eq: chevre.factory.actionStatusType.CompletedActionStatus },
            sort: { startDate: chevre.factory.sortType.Descending }
        }
    );
    // tslint:disable-next-line:no-null-keyword
    console.dir(docs, { depth: null });
    // tslint:disable-next-line:no-null-keyword
    console.dir(docs.map(({ typeOf, startDate }) => ({ typeOf, startDate })), { depth: null });
    console.log(docs.length, 'docs found');
}

main()
    .then()
    .catch(console.error);
