fix the company logo path issue
This commit is contained in:
@@ -395,6 +395,17 @@ const s3 = new AWS.S3({
|
||||
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 = {
|
||||
firstName?: string;
|
||||
lastName?: string | null;
|
||||
@@ -415,6 +426,43 @@ type UpdateHostProfileInput = {
|
||||
export class HostService {
|
||||
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) {
|
||||
return this.prisma.user.create({ data });
|
||||
}
|
||||
@@ -457,12 +505,6 @@ export class HostService {
|
||||
gstNumber: true,
|
||||
formationDate: true,
|
||||
companyTypeXid: true,
|
||||
companyTypes: {
|
||||
select: {
|
||||
id: true,
|
||||
companyTypeName: true
|
||||
}
|
||||
},
|
||||
websiteUrl: true,
|
||||
instagramUrl: true,
|
||||
facebookUrl: true,
|
||||
@@ -656,11 +698,15 @@ export class HostService {
|
||||
}
|
||||
|
||||
if (host?.logoPath) {
|
||||
const key = host.logoPath.startsWith('http')
|
||||
? host.logoPath.split('.com/')[1]
|
||||
: host.logoPath;
|
||||
|
||||
(host as any).logoPresignedUrl = await getPresignedUrl(bucket, key);
|
||||
const resolvedLogoUrl = await this.getValidLogoUrl(
|
||||
'hostHeader',
|
||||
host.id,
|
||||
host.logoPath,
|
||||
);
|
||||
if (!resolvedLogoUrl) {
|
||||
host.logoPath = null;
|
||||
}
|
||||
(host as any).logoPresignedUrl = resolvedLogoUrl;
|
||||
}
|
||||
|
||||
if (host.accountManager?.profileImage) {
|
||||
@@ -676,11 +722,15 @@ export class HostService {
|
||||
|
||||
// Parent company logo
|
||||
if (parent.logoPath) {
|
||||
const key = parent.logoPath.startsWith('http')
|
||||
? parent.logoPath.split('.com/')[1]
|
||||
: parent.logoPath;
|
||||
|
||||
(parent as any).logoPresignedUrl = await getPresignedUrl(bucket, key);
|
||||
const resolvedParentLogoUrl = await this.getValidLogoUrl(
|
||||
'hostParent',
|
||||
parent.id,
|
||||
parent.logoPath,
|
||||
);
|
||||
if (!resolvedParentLogoUrl) {
|
||||
parent.logoPath = null;
|
||||
}
|
||||
(parent as any).logoPresignedUrl = resolvedParentLogoUrl;
|
||||
}
|
||||
|
||||
// Parent documents
|
||||
@@ -1697,8 +1747,10 @@ export class HostService {
|
||||
: undefined,
|
||||
pinCode: companyData.pinCode,
|
||||
logoPath: options?.deleteCompanyLogo
|
||||
? companyData.logoPath ?? null
|
||||
: companyData.logoPath || existingHostCompany.logoPath,
|
||||
? null
|
||||
: resolveIncomingLogoPath(companyData.logoPath) ??
|
||||
existingHostCompany.logoPath ??
|
||||
null,
|
||||
isSubsidairy: companyData.isSubsidairy,
|
||||
registrationNumber: companyData.registrationNumber,
|
||||
panNumber: companyData.panNumber,
|
||||
@@ -1840,9 +1892,9 @@ export class HostService {
|
||||
: undefined,
|
||||
pinCode: parentCompanyData.pinCode || null,
|
||||
logoPath: options?.deleteParentCompanyLogo
|
||||
? parentCompanyData?.logoPath ?? null
|
||||
: parentCompanyData?.logoPath ||
|
||||
existingParentCompany?.logoPath ||
|
||||
? null
|
||||
: resolveIncomingLogoPath(parentCompanyData?.logoPath) ??
|
||||
existingParentCompany?.logoPath ??
|
||||
null,
|
||||
registrationNumber: parentCompanyData.registrationNumber || null,
|
||||
panNumber: parentCompanyData.panNumber || null,
|
||||
@@ -1899,9 +1951,9 @@ export class HostService {
|
||||
: undefined,
|
||||
pinCode: parentCompanyData.pinCode || null,
|
||||
logoPath: options?.deleteParentCompanyLogo
|
||||
? parentCompanyData?.logoPath ?? null
|
||||
: parentCompanyData?.logoPath ||
|
||||
existingParentCompany?.logoPath ||
|
||||
? null
|
||||
: resolveIncomingLogoPath(parentCompanyData?.logoPath) ??
|
||||
existingParentCompany?.logoPath ??
|
||||
null,
|
||||
registrationNumber: parentCompanyData.registrationNumber || null,
|
||||
panNumber: parentCompanyData.panNumber || null,
|
||||
|
||||
Reference in New Issue
Block a user