chore(global): remove a bunch of test and temp files
This commit is contained in:
@@ -4,6 +4,7 @@ import { companies } from "../../../managers/companies";
|
||||
import { apiResponse } from "../../../modules/api-utils/apiResponse";
|
||||
import { ContentfulStatusCode } from "hono/utils/http-status";
|
||||
import { authMiddleware } from "../../middleware/authorization";
|
||||
import GenericError from "../../../Errors/GenericError";
|
||||
import { processObjectValuePerms } from "../../../modules/permission-utils/processObjectPermissions";
|
||||
|
||||
/* /v1/company/companies/[id] */
|
||||
@@ -12,18 +13,37 @@ export default createRoute(
|
||||
["/companies/:identifier"],
|
||||
|
||||
async (c) => {
|
||||
const company = await companies.fetch(c.req.param("identifier"));
|
||||
const user = c.get("user");
|
||||
const includeAddress =
|
||||
c.req.query("includeAddress") === "true" &&
|
||||
!!user &&
|
||||
(await user.hasPermission("company.fetch.address"));
|
||||
const company = await companies.fetch(c.req.param("identifier") as string);
|
||||
const includeAddress = c.req.query("includeAddress") === "true";
|
||||
const includePrimaryContact =
|
||||
c.req.query("includePrimaryContact") === "true";
|
||||
const includeAllContacts =
|
||||
c.req.query("includeAllContacts") === "true" &&
|
||||
!!user &&
|
||||
(await user.hasPermission("company.fetch.contacts"));
|
||||
const includeAllContacts = c.req.query("includeAllContacts") === "true";
|
||||
|
||||
console.log(company.toJson({ includeAddress, includePrimaryContact, includeAllContacts }));
|
||||
|
||||
// Check for address-specific permission if includeAddress is requested
|
||||
if (includeAddress) {
|
||||
const user = c.get("user");
|
||||
if (!user || !(await user.hasPermission("company.fetch.address"))) {
|
||||
throw new GenericError({
|
||||
name: "InsufficientPermission",
|
||||
message: "You do not have permission to view company addresses.",
|
||||
status: 403,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Check for contacts permission if includeAllContacts is requested
|
||||
if (includeAllContacts) {
|
||||
const user = c.get("user");
|
||||
if (!user || !(await user.hasPermission("company.fetch.contacts"))) {
|
||||
throw new GenericError({
|
||||
name: "InsufficientPermission",
|
||||
message: "You do not have permission to view company contacts.",
|
||||
status: 403,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const companyData = company.toJson({
|
||||
includeAddress,
|
||||
@@ -36,13 +56,6 @@ export default createRoute(
|
||||
c.get("user"),
|
||||
);
|
||||
|
||||
// cw_Data fields were already gated by the explicit permission checks above
|
||||
// (company.fetch.contacts / company.fetch.address). Re-attach them so they
|
||||
// are not silently dropped by field-level gating on obj.company.cw_Data.
|
||||
if (companyData.cw_Data && Object.keys(companyData.cw_Data).length > 0) {
|
||||
(gatedData as any).cw_Data = companyData.cw_Data;
|
||||
}
|
||||
|
||||
const response = apiResponse.successful(
|
||||
"Company Fetched Successfully!",
|
||||
gatedData,
|
||||
|
||||
@@ -274,7 +274,7 @@ export class CompanyController {
|
||||
(ci) => ci.type?.name === "Email"
|
||||
);
|
||||
return {
|
||||
id: contact.id,
|
||||
cwId: contact.id,
|
||||
firstName: contact.firstName,
|
||||
lastName: contact.lastName,
|
||||
inactive: contact.inactiveFlag ?? false,
|
||||
@@ -303,16 +303,41 @@ export class CompanyController {
|
||||
};
|
||||
}
|
||||
|
||||
if (opts?.includePrimaryContact && this.cw_Data?.defaultContact) {
|
||||
cw_Data.primaryContact = this._serializeContact(
|
||||
this.cw_Data.defaultContact
|
||||
);
|
||||
if (opts?.includePrimaryContact) {
|
||||
if (this.cw_Data?.defaultContact) {
|
||||
cw_Data.primaryContact = this._serializeContact(
|
||||
this.cw_Data.defaultContact
|
||||
);
|
||||
} else if (this._defaultContact) {
|
||||
const c = this._defaultContact;
|
||||
cw_Data.primaryContact = {
|
||||
cwId: c.id,
|
||||
firstName: c.firstName,
|
||||
lastName: c.lastName,
|
||||
inactive: !c.active,
|
||||
title: c.title ?? null,
|
||||
phone: c.phone ?? null,
|
||||
email: c.email ?? null,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (opts?.includeAllContacts && this.cw_Data?.allContacts) {
|
||||
cw_Data.allContacts = this.cw_Data.allContacts.map((c) =>
|
||||
this._serializeContact(c)
|
||||
);
|
||||
if (opts?.includeAllContacts) {
|
||||
if (this.cw_Data?.allContacts) {
|
||||
cw_Data.allContacts = this.cw_Data.allContacts.map((c) =>
|
||||
this._serializeContact(c)
|
||||
);
|
||||
} else if (this._contacts.length > 0) {
|
||||
cw_Data.allContacts = this._contacts.map((c) => ({
|
||||
cwId: c.id,
|
||||
firstName: c.firstName,
|
||||
lastName: c.lastName,
|
||||
inactive: !c.active,
|
||||
title: c.title ?? null,
|
||||
phone: c.phone ?? null,
|
||||
email: c.email ?? null,
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
|
||||
@@ -46,7 +46,7 @@ export const opportunities = {
|
||||
|
||||
// Resolve optional local FKs — nullify any that don't exist locally yet
|
||||
// (the sync may be behind; these are all nullable in the schema)
|
||||
const [companyExists, contactExists, siteExists] = await Promise.all([
|
||||
const [companyExists, contactExists, siteExists, typeExists] = await Promise.all([
|
||||
cwData.company?.id
|
||||
? prisma.company.findFirst({ where: { id: cwData.company.id }, select: { id: true } })
|
||||
: null,
|
||||
@@ -56,16 +56,21 @@ export const opportunities = {
|
||||
mapped.siteId != null
|
||||
? prisma.companyAddress.findFirst({ where: { id: mapped.siteId }, select: { id: true } })
|
||||
: null,
|
||||
mapped.typeId != null
|
||||
? prisma.opportunityType.findFirst({ where: { id: mapped.typeId }, select: { id: true } })
|
||||
: null,
|
||||
]);
|
||||
|
||||
const companyId = companyExists?.id ?? null;
|
||||
const contactId = contactExists?.id ?? null;
|
||||
const siteId = siteExists?.id ?? null;
|
||||
const typeId = typeExists?.id ?? null;
|
||||
|
||||
const record = await prisma.opportunity.create({
|
||||
data: {
|
||||
id: cwData.id,
|
||||
...mapped,
|
||||
typeId,
|
||||
companyId,
|
||||
contactId,
|
||||
siteId,
|
||||
|
||||
Reference in New Issue
Block a user