Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/app/core/guards/chat-config/chat-config.guard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Injectable } from '@angular/core';
import { CanActivate, Router, UrlTree } from '@angular/router';
import { localKeys } from 'src/app/core/constants/localStorage.keys';
import { LocalStorageService } from 'src/app/core/services';
import { CommonRoutes } from 'src/global.routes';

@Injectable({
providedIn: 'root',
})
export class ChatConfigGuard implements CanActivate {
constructor(
private localStorage: LocalStorageService,
private router: Router
) {}

async canActivate(): Promise<boolean | UrlTree> {
const chatConfig = await this.localStorage.getLocalData(
localKeys.CHAT_CONFIG
);
const isEnabled = String(chatConfig) === 'true';

if (isEnabled) {
return true;
}

return this.router.parseUrl(`/${CommonRoutes.TABS}/${CommonRoutes.HOME}`);
}
}
30 changes: 29 additions & 1 deletion src/app/core/services/appinit.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ export class PrivateService {
adminAccess = signal<boolean>(false);
userRoles = signal<any>(null);

private readonly allAppPages = signal(_.cloneDeep(APP_PAGES));
public appPages = signal(_.cloneDeep(APP_PAGES));
public adminPage = ADMIN_PAGE;
chatConfig: any;

actionsArrays: any[] = permissionModule.MODULES;
userEventSubscription: any;
Expand Down Expand Up @@ -85,14 +87,15 @@ export class PrivateService {
)
: false);
}
await this.permissionService.getPlatformConfig();
await this.profile.getChatToken();
this.getUser();
resolve();
}, 0);
});

this.db.init();

await new Promise<void>((resolve) => {
setTimeout(async () => {
this.userRoles.set(await this.localStorage.getLocalData(
Expand Down Expand Up @@ -136,6 +139,31 @@ export class PrivateService {

this.subscribeBackButton();
await this.checkBadges();
this.chatConfig = await this.localStorage.getLocalData(localKeys['CHAT_CONFIG']);
this.syncVisibleAppPages();
}

private isChatEnabled(): boolean {
const isEnabled = String(this.chatConfig) === 'true';
return isEnabled;
}

private syncVisibleAppPages(): void {
const isChatEnabled = this.isChatEnabled();
const pages = this.appPages().map((page: any) => {
if ([PAGE_IDS.myConnections, PAGE_IDS.messages].includes(page.pageId)) {
return {
...page,
showTab: isChatEnabled,
};
}

return {
...page,
showTab: page.showTab ?? true,
};
});
this.appPages.set(pages);
}

applyTheme() {
Expand Down
102 changes: 102 additions & 0 deletions src/app/core/services/file-upload/file-upload.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import { TestBed } from '@angular/core/testing';
import { of, throwError } from 'rxjs';
import { AttachmentService } from '../attachment/attachment.service';
import { FileUploadService } from './file-upload.service';

describe('FileUploadService', () => {
let service: FileUploadService;
let attachmentSpy: jasmine.SpyObj<AttachmentService>;

beforeEach(() => {
attachmentSpy = jasmine.createSpyObj('AttachmentService', [
'cloudImageUpload',
'getImageUploadUrl'
]);

TestBed.configureTestingModule({
providers: [
FileUploadService,
{ provide: AttachmentService, useValue: attachmentSpy }
]
});

service = TestBed.inject(FileUploadService);
});

it('uploadFile should resolve with destination file path after upload', async () => {
const file = new File(['content'], 'resource.pdf', { type: 'application/pdf' });
const signedUrl = { destFilePath: 'https://cdn.test/resource.pdf' };
attachmentSpy.cloudImageUpload.and.returnValue(of({}));

const result = await service.uploadFile(file, signedUrl);

expect(attachmentSpy.cloudImageUpload).toHaveBeenCalledWith(file, signedUrl);
expect(result).toBe('https://cdn.test/resource.pdf');
});

it('uploadFile should reject when upload errors', async () => {
const file = new File(['content'], 'resource.pdf', { type: 'application/pdf' });
const signedUrl = { destFilePath: 'https://cdn.test/resource.pdf' };
attachmentSpy.cloudImageUpload.and.returnValue(throwError(() => 'upload failed'));

await expectAsync(service.uploadFile(file, signedUrl)).toBeRejectedWith('upload failed');
});

it('handleFileUploads should transform links, upload files, and preserve existing entries', async () => {
const file = new File(['content'], 'resource.pdf', { type: 'application/pdf' });
const signedUrl = { destFilePath: 'https://cdn.test/resource.pdf' };
attachmentSpy.getImageUploadUrl.and.returnValue(Promise.resolve(signedUrl));
attachmentSpy.cloudImageUpload.and.returnValue(of({}));

const result = await service.handleFileUploads([
{
name: 'resources',
type: 'search',
meta: { addPopupType: 'file' },
value: [
{ name: 'Google', isLink: true, link: 'https://google.com' },
{ name: 'PDF', file },
{ name: 'Existing', link: 'https://existing.test', type: 'resources', mime_type: 'link' }
]
},
{
name: 'ignored',
type: 'text',
meta: { addPopupType: 'file' },
value: [{ name: 'Nope' }]
}
]);

expect(attachmentSpy.getImageUploadUrl).toHaveBeenCalledWith(file);
expect(result).toEqual([
{
name: 'Google',
link: 'https://google.com',
type: 'resources',
mime_type: 'link'
},
{
name: 'PDF',
link: 'https://cdn.test/resource.pdf',
type: 'resources',
mime_type: 'application/pdf'
},
{
name: 'Existing',
link: 'https://existing.test',
type: 'resources',
mime_type: 'link'
}
]);
});

it('handleFileUploads should return an empty list when no uploadable file controls exist', async () => {
const result = await service.handleFileUploads([
{ type: 'text', meta: {}, value: [] },
{ type: 'search', meta: { addPopupType: 'user' }, value: [{ id: 1 }] }
]);

expect(result).toEqual([]);
expect(attachmentSpy.getImageUploadUrl).not.toHaveBeenCalled();
});
});
21 changes: 18 additions & 3 deletions src/app/core/services/profile/profile.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export class ProfileService {
private modal: ModalController,
private chatService: FrontendChatLibraryService
) {}
async profileUpdate(formData, showToast = true) {
async profileUpdate(formData, showToast = true) {
await this.loaderService.startLoader();
const config = {
url: urlConstants.API_URLS.PROFILE_UPDATE,
Expand Down Expand Up @@ -157,19 +157,29 @@ export class ProfileService {
}
}

async getProfileDetailsFromAPI() {
async getProfileDetailsFromAPI(forceRefresh = false) {
if (!forceRefresh) {
const cachedUserDetails = await this.localStorage.getLocalData(
localKeys.USER_DETAILS
);
if (cachedUserDetails) {
return cachedUserDetails;
}
}

const config = {
url: urlConstants.API_URLS.PROFILE_READ,
payload: {},
};
try {
let data: any = await this.httpService.get(config);
data = _.get(data, 'result');
const userRole = this.getUserRole(data);
this.getUserRole(data);
await this.localStorage.setLocalData(localKeys.USER_DETAILS, data);
await this.localStorage.setLocalData(
localKeys.USER_ROLES,
this.getUserRole(data)
userRole
);
return data;
} catch (error) {}
Expand Down Expand Up @@ -284,6 +294,11 @@ export class ProfileService {
const config = {
url: urlConstants.API_URLS.GET_CHAT_TOKEN,
};
const chatConfig = await this.localStorage.getLocalData(localKeys['CHAT_CONFIG'])
const isEnabled = String(chatConfig) === 'true';

if (!isEnabled) return;

try {
const resp = await this.httpService.get(config);
if (resp.result) {
Expand Down
Loading
Loading