104 lines
2.5 KiB
TypeScript
104 lines
2.5 KiB
TypeScript
"use strict";
|
|
import {
|
|
Context,
|
|
Service,
|
|
ServiceBroker,
|
|
ServiceSchema,
|
|
Errors,
|
|
} from "moleculer";
|
|
import { DbMixin } from "../mixins/db.mixin";
|
|
import Comic from "../models/comic.model";
|
|
import { walkFolder } from "../utils/file.utils";
|
|
import { convertXMLToJSON } from "../utils/xml.utils";
|
|
|
|
export default class ProductsService extends Service {
|
|
public constructor(
|
|
public broker: ServiceBroker,
|
|
schema: ServiceSchema<{}> = { name: "import" }
|
|
) {
|
|
super(broker);
|
|
this.parseServiceSchema(
|
|
Service.mergeSchemas(
|
|
{
|
|
name: "import",
|
|
mixins: [DbMixin("comics", Comic)],
|
|
settings: {
|
|
// Available fields in the responses
|
|
fields: ["_id", "name", "quantity", "price"],
|
|
|
|
// Validator for the `create` & `insert` actions.
|
|
entityValidator: {
|
|
name: "string|min:3",
|
|
price: "number|positive",
|
|
},
|
|
},
|
|
hooks: {},
|
|
actions: {
|
|
walkFolders: {
|
|
rest: "POST /walkFolders",
|
|
params: {
|
|
basePathToWalk: "string",
|
|
},
|
|
async handler(
|
|
ctx: Context<{ basePathToWalk: string }>
|
|
) {
|
|
return await walkFolder(
|
|
ctx.params.basePathToWalk
|
|
);
|
|
},
|
|
},
|
|
convertXMLToJSON: {
|
|
rest: "POST /convertXmlToJson",
|
|
params: {},
|
|
async handler(ctx: Context<{}>) {
|
|
return convertXMLToJSON("lagos");
|
|
},
|
|
},
|
|
rawImportToDB: {
|
|
rest: "POST /rawImportToDB",
|
|
params: {},
|
|
async handler(ctx: Context<{ payload: object }>) {
|
|
return new Promise((resolve, reject) => {
|
|
Comic.create(ctx.params, (error, data) => {
|
|
if (data) {
|
|
resolve(data);
|
|
} else if (error) {
|
|
throw new Errors.MoleculerError(
|
|
"Failed to import comic book",
|
|
400,
|
|
"IMS_FAILED_COMIC_BOOK_IMPORT",
|
|
data
|
|
);
|
|
}
|
|
});
|
|
});
|
|
},
|
|
},
|
|
getRecentlyImportedComicBooks: {
|
|
rest: "POST /getRecentlyImportedComicBooks",
|
|
params: {},
|
|
async handler(
|
|
ctx: Context<{ paginationOptions: object }>
|
|
) {
|
|
return await Comic.paginate(
|
|
{},
|
|
ctx.params.paginationOptions
|
|
);
|
|
},
|
|
},
|
|
getComicBookById: {
|
|
rest: "POST /getComicBookById",
|
|
params: { id: "string" },
|
|
async handler(ctx: Context<{ id: string }>) {
|
|
return await Comic.findById(ctx.params.id);
|
|
},
|
|
},
|
|
},
|
|
methods: {},
|
|
},
|
|
schema
|
|
)
|
|
);
|
|
}
|
|
}
|