Setup fetch and wip fetchall route for getting companies.
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
import { Hono } from "hono/tiny";
|
||||
import { createRoute } from "../../modules/api-utils/createRoute";
|
||||
import { companies } from "../../managers/companies";
|
||||
import { apiResponse } from "../../modules/api-utils/apiResponse";
|
||||
import { ContentfulStatusCode } from "hono/utils/http-status";
|
||||
import { authMiddleware } from "../middleware/authorization";
|
||||
|
||||
/* /v1/company/[id] */
|
||||
export default createRoute(
|
||||
"get",
|
||||
["/:identifier"],
|
||||
async (c) => {
|
||||
const company = await companies.fetch(c.req.param("identifier"));
|
||||
|
||||
const response = apiResponse.successful(
|
||||
"Company Fetched Successfully!",
|
||||
company,
|
||||
);
|
||||
return c.json(response, response.status as ContentfulStatusCode);
|
||||
},
|
||||
authMiddleware({ permissions: ["company.fetch"] }),
|
||||
);
|
||||
@@ -0,0 +1,31 @@
|
||||
import { Hono } from "hono/tiny";
|
||||
import { createRoute } from "../../modules/api-utils/createRoute";
|
||||
import { companies } from "../../managers/companies";
|
||||
import { apiResponse } from "../../modules/api-utils/apiResponse";
|
||||
import { ContentfulStatusCode } from "hono/utils/http-status";
|
||||
import { authMiddleware } from "../middleware/authorization";
|
||||
|
||||
/* /v1/company/companies */
|
||||
export default createRoute(
|
||||
"get",
|
||||
["/companies"],
|
||||
async (c) => {
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @TODO MAKE THIS WORK
|
||||
*
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
const company = await companies.fetch(c.req.param("identifier"));
|
||||
|
||||
const response = apiResponse.successful(
|
||||
"Company Fetched Successfully!",
|
||||
company,
|
||||
);
|
||||
return c.json(response, response.status as ContentfulStatusCode);
|
||||
},
|
||||
authMiddleware({ permissions: ["company.fetch"] }),
|
||||
);
|
||||
@@ -0,0 +1,47 @@
|
||||
import { Company } from "../../generated/prisma/client";
|
||||
import { updateCwInternalCompany } from "../modules/cw-utils/updateCompany";
|
||||
|
||||
/**
|
||||
* Company Controller
|
||||
*
|
||||
* This class is for creating a controller that can manage company data,
|
||||
* synchronize with external systems, and provide methods for accessing
|
||||
* and updating company information within our internal system.
|
||||
*/
|
||||
export class CompanyController {
|
||||
public readonly id: string;
|
||||
public name: string;
|
||||
public readonly cw_Identifier: string;
|
||||
public readonly cw_CompanyId: number;
|
||||
|
||||
constructor(companyData: Company) {
|
||||
this.id = companyData.id;
|
||||
this.name = companyData.name;
|
||||
this.cw_Identifier = companyData.cw_Identifier;
|
||||
this.cw_CompanyId = companyData.cw_CompanyId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Refresh Internal Company Data from ConnectWise
|
||||
*
|
||||
* This method fetches the latest company data from ConnectWise and updates
|
||||
* the internal company information accordingly.
|
||||
*
|
||||
* @returns {ThisType} - Updated Controller
|
||||
*/
|
||||
public async refreshFromCW() {
|
||||
const data = await updateCwInternalCompany(this.cw_CompanyId);
|
||||
|
||||
this.name = data?.name || this.name;
|
||||
return this;
|
||||
}
|
||||
|
||||
public toJson() {
|
||||
return {
|
||||
id: this.id,
|
||||
name: this.name,
|
||||
cw_Identifier: this.cw_Identifier,
|
||||
cw_CompanyId: this.cw_CompanyId,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
import { prisma } from "../constants";
|
||||
import { CompanyController } from "../controllers/CompanyController";
|
||||
|
||||
export const companies = {
|
||||
async fetch(identifier: string | number): Promise<CompanyController> {
|
||||
const search = await prisma.company.findFirst({
|
||||
where: {
|
||||
OR: [
|
||||
{ id: identifier as string },
|
||||
{ cw_CompanyId: identifier as number },
|
||||
{ cw_Identifier: identifier as string },
|
||||
],
|
||||
},
|
||||
});
|
||||
|
||||
if (!search) throw new Error("Unknown company.");
|
||||
|
||||
return new CompanyController(search);
|
||||
},
|
||||
};
|
||||
@@ -0,0 +1,19 @@
|
||||
import { connectWiseApi } from "../../constants";
|
||||
import { Company } from "../../types/ConnectWiseTypes";
|
||||
|
||||
export const fetchCwCompanyById = async (
|
||||
companyId: number,
|
||||
): Promise<Company | null> => {
|
||||
try {
|
||||
const response = await connectWiseApi.get(
|
||||
`/company/companies/${companyId}`,
|
||||
);
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
console.error(
|
||||
`Error fetching company with ID ${companyId}:`,
|
||||
(error as any).response?.data || error,
|
||||
);
|
||||
return null;
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,24 @@
|
||||
import { Company } from "../../../generated/prisma/client";
|
||||
import { prisma } from "../../constants";
|
||||
import { fetchCwCompanyById } from "./fetchCompany";
|
||||
|
||||
export const updateCwInternalCompany = async (
|
||||
companyId: number,
|
||||
): Promise<Company | null> => {
|
||||
const cwCompany = await fetchCwCompanyById(companyId);
|
||||
if (!cwCompany) return null;
|
||||
|
||||
const updatedCompany = await prisma.company.upsert({
|
||||
where: { cw_CompanyId: cwCompany.id },
|
||||
create: {
|
||||
cw_CompanyId: cwCompany.id,
|
||||
cw_Identifier: cwCompany.identifier,
|
||||
name: cwCompany.name,
|
||||
},
|
||||
update: {
|
||||
name: cwCompany.name,
|
||||
},
|
||||
});
|
||||
|
||||
return updatedCompany;
|
||||
};
|
||||
Reference in New Issue
Block a user