fix the company logo path issue
This commit is contained in:
@@ -395,6 +395,17 @@ const s3 = new AWS.S3({
|
|||||||
region: config.aws.region,
|
region: config.aws.region,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
function getS3KeyFromStoredPath(path?: string | null) {
|
||||||
|
if (!path) return null;
|
||||||
|
return path.startsWith('http') ? path.split('.com/')[1] || null : path;
|
||||||
|
}
|
||||||
|
|
||||||
|
function resolveIncomingLogoPath(path?: string | null) {
|
||||||
|
if (typeof path !== 'string') return null;
|
||||||
|
const trimmed = path.trim();
|
||||||
|
return trimmed.length ? trimmed : null;
|
||||||
|
}
|
||||||
|
|
||||||
type UpdateHostProfileInput = {
|
type UpdateHostProfileInput = {
|
||||||
firstName?: string;
|
firstName?: string;
|
||||||
lastName?: string | null;
|
lastName?: string | null;
|
||||||
@@ -415,6 +426,43 @@ type UpdateHostProfileInput = {
|
|||||||
export class HostService {
|
export class HostService {
|
||||||
constructor(private prisma: PrismaClient) { }
|
constructor(private prisma: PrismaClient) { }
|
||||||
|
|
||||||
|
private async getValidLogoUrl(
|
||||||
|
model: 'hostHeader' | 'hostParent',
|
||||||
|
recordId: number,
|
||||||
|
logoPath?: string | null,
|
||||||
|
) {
|
||||||
|
const key = getS3KeyFromStoredPath(logoPath);
|
||||||
|
if (!key) return null;
|
||||||
|
|
||||||
|
try {
|
||||||
|
await s3
|
||||||
|
.headObject({
|
||||||
|
Bucket: bucket,
|
||||||
|
Key: key,
|
||||||
|
})
|
||||||
|
.promise();
|
||||||
|
|
||||||
|
return await getPresignedUrl(bucket, key);
|
||||||
|
} catch (error: any) {
|
||||||
|
const statusCode = error?.statusCode;
|
||||||
|
const errorCode = error?.code;
|
||||||
|
const isMissingObject =
|
||||||
|
statusCode === 404 ||
|
||||||
|
errorCode === 'NotFound' ||
|
||||||
|
errorCode === 'NoSuchKey';
|
||||||
|
|
||||||
|
if (isMissingObject) {
|
||||||
|
await (this.prisma as any)[model].update({
|
||||||
|
where: { id: recordId },
|
||||||
|
data: { logoPath: null },
|
||||||
|
});
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async createHost(data: CreateHostDto) {
|
async createHost(data: CreateHostDto) {
|
||||||
return this.prisma.user.create({ data });
|
return this.prisma.user.create({ data });
|
||||||
}
|
}
|
||||||
@@ -457,12 +505,6 @@ export class HostService {
|
|||||||
gstNumber: true,
|
gstNumber: true,
|
||||||
formationDate: true,
|
formationDate: true,
|
||||||
companyTypeXid: true,
|
companyTypeXid: true,
|
||||||
companyTypes: {
|
|
||||||
select: {
|
|
||||||
id: true,
|
|
||||||
companyTypeName: true
|
|
||||||
}
|
|
||||||
},
|
|
||||||
websiteUrl: true,
|
websiteUrl: true,
|
||||||
instagramUrl: true,
|
instagramUrl: true,
|
||||||
facebookUrl: true,
|
facebookUrl: true,
|
||||||
@@ -656,11 +698,15 @@ export class HostService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (host?.logoPath) {
|
if (host?.logoPath) {
|
||||||
const key = host.logoPath.startsWith('http')
|
const resolvedLogoUrl = await this.getValidLogoUrl(
|
||||||
? host.logoPath.split('.com/')[1]
|
'hostHeader',
|
||||||
: host.logoPath;
|
host.id,
|
||||||
|
host.logoPath,
|
||||||
(host as any).logoPresignedUrl = await getPresignedUrl(bucket, key);
|
);
|
||||||
|
if (!resolvedLogoUrl) {
|
||||||
|
host.logoPath = null;
|
||||||
|
}
|
||||||
|
(host as any).logoPresignedUrl = resolvedLogoUrl;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (host.accountManager?.profileImage) {
|
if (host.accountManager?.profileImage) {
|
||||||
@@ -676,11 +722,15 @@ export class HostService {
|
|||||||
|
|
||||||
// Parent company logo
|
// Parent company logo
|
||||||
if (parent.logoPath) {
|
if (parent.logoPath) {
|
||||||
const key = parent.logoPath.startsWith('http')
|
const resolvedParentLogoUrl = await this.getValidLogoUrl(
|
||||||
? parent.logoPath.split('.com/')[1]
|
'hostParent',
|
||||||
: parent.logoPath;
|
parent.id,
|
||||||
|
parent.logoPath,
|
||||||
(parent as any).logoPresignedUrl = await getPresignedUrl(bucket, key);
|
);
|
||||||
|
if (!resolvedParentLogoUrl) {
|
||||||
|
parent.logoPath = null;
|
||||||
|
}
|
||||||
|
(parent as any).logoPresignedUrl = resolvedParentLogoUrl;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parent documents
|
// Parent documents
|
||||||
@@ -1697,8 +1747,10 @@ export class HostService {
|
|||||||
: undefined,
|
: undefined,
|
||||||
pinCode: companyData.pinCode,
|
pinCode: companyData.pinCode,
|
||||||
logoPath: options?.deleteCompanyLogo
|
logoPath: options?.deleteCompanyLogo
|
||||||
? companyData.logoPath ?? null
|
? null
|
||||||
: companyData.logoPath || existingHostCompany.logoPath,
|
: resolveIncomingLogoPath(companyData.logoPath) ??
|
||||||
|
existingHostCompany.logoPath ??
|
||||||
|
null,
|
||||||
isSubsidairy: companyData.isSubsidairy,
|
isSubsidairy: companyData.isSubsidairy,
|
||||||
registrationNumber: companyData.registrationNumber,
|
registrationNumber: companyData.registrationNumber,
|
||||||
panNumber: companyData.panNumber,
|
panNumber: companyData.panNumber,
|
||||||
@@ -1840,9 +1892,9 @@ export class HostService {
|
|||||||
: undefined,
|
: undefined,
|
||||||
pinCode: parentCompanyData.pinCode || null,
|
pinCode: parentCompanyData.pinCode || null,
|
||||||
logoPath: options?.deleteParentCompanyLogo
|
logoPath: options?.deleteParentCompanyLogo
|
||||||
? parentCompanyData?.logoPath ?? null
|
? null
|
||||||
: parentCompanyData?.logoPath ||
|
: resolveIncomingLogoPath(parentCompanyData?.logoPath) ??
|
||||||
existingParentCompany?.logoPath ||
|
existingParentCompany?.logoPath ??
|
||||||
null,
|
null,
|
||||||
registrationNumber: parentCompanyData.registrationNumber || null,
|
registrationNumber: parentCompanyData.registrationNumber || null,
|
||||||
panNumber: parentCompanyData.panNumber || null,
|
panNumber: parentCompanyData.panNumber || null,
|
||||||
@@ -1899,9 +1951,9 @@ export class HostService {
|
|||||||
: undefined,
|
: undefined,
|
||||||
pinCode: parentCompanyData.pinCode || null,
|
pinCode: parentCompanyData.pinCode || null,
|
||||||
logoPath: options?.deleteParentCompanyLogo
|
logoPath: options?.deleteParentCompanyLogo
|
||||||
? parentCompanyData?.logoPath ?? null
|
? null
|
||||||
: parentCompanyData?.logoPath ||
|
: resolveIncomingLogoPath(parentCompanyData?.logoPath) ??
|
||||||
existingParentCompany?.logoPath ||
|
existingParentCompany?.logoPath ??
|
||||||
null,
|
null,
|
||||||
registrationNumber: parentCompanyData.registrationNumber || null,
|
registrationNumber: parentCompanyData.registrationNumber || null,
|
||||||
panNumber: parentCompanyData.panNumber || null,
|
panNumber: parentCompanyData.panNumber || null,
|
||||||
|
|||||||
Reference in New Issue
Block a user