Angular app for CAC desktop
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 

375 rader
16 KiB

  1. import { Component, OnInit } from '@angular/core';
  2. import { PartnerProfileService } from '../../services/partner-profile.service';
  3. import { Router } from '@angular/router';
  4. import * as Papa from 'papaparse';
  5. import * as XLSX from 'xlsx'
  6. import { UserData, UserDataOption } from 'src/shared/structure/user';
  7. import { COUNTRIES, STATES, COMMUNITIES } from 'src/shared/data/form-options';
  8. import { PartnerType, PARTNER_MAPPING } from 'src/shared/data/partner-mapping';
  9. type exportType = "CSV" | "XLSX";
  10. @Component({
  11. selector: 'app-table',
  12. templateUrl: './table.component.html',
  13. styleUrls: ['./table.component.scss']
  14. })
  15. export class TableComponent implements OnInit {
  16. userData: Array<UserData> = [];
  17. tempUserData: Array<UserData> = [];
  18. showExportOptions: boolean = false;
  19. shouldHaveImplementationData: boolean = false;
  20. searchText: string = '';
  21. selectedPartnerList: Array<UserData> = [];
  22. isProfileData: boolean = true;
  23. isImplementationData: boolean = false;
  24. isBothData: boolean = false;
  25. communities = COMMUNITIES;
  26. countries = COUNTRIES;
  27. states = STATES;
  28. districts: Array<UserDataOption> = [];
  29. partnerTypes = [PartnerType.ENABLER, PartnerType.IMPLEMENTER, PartnerType.PROVIDER];
  30. filteredCommunities: Array<string> = [];
  31. filteredCountries: Array<string> = [];
  32. filteredStates: Array<string> = [];
  33. filteredDistricts: Array<string> = [];
  34. filteredPartnerTypes: Array<PartnerType> = [];
  35. exportData: Array<any> = [];
  36. exportSurveyCtoData: Array<any> = [];
  37. constructor(
  38. private partnerProfileService: PartnerProfileService,
  39. private router: Router
  40. ) { }
  41. ngOnInit(): void {
  42. this.getFilteredData();
  43. }
  44. updateDistricts(states: Array<{ districts: Array<UserDataOption> }>) {
  45. console.log(states);
  46. this.districts = states.map(state => state.districts).flat();
  47. this.getFilteredData();
  48. }
  49. getFilteredData() {
  50. this.partnerProfileService.getPartnersData().then((data) => {
  51. this.userData = data;
  52. const searchText = this.searchText.toLowerCase().trim();
  53. const filteredCommunitySet = new Set(this.filteredCommunities);
  54. const filteredCountrySet = new Set(this.filteredCountries);
  55. const filteredStateSet = new Set(this.filteredStates);
  56. const filteredDistrictSet = new Set(this.filteredDistricts);
  57. const filteredPartnerTypeSet = new Set(this.filteredPartnerTypes);
  58. this.userData = this.userData.filter(user => {
  59. if (this.shouldHaveImplementationData) {
  60. const hasImplementationData = (user.surveyCtoData.hiiData && user.surveyCtoData.hiiData.length > 0) ||
  61. (user.surveyCtoData.spData && user.surveyCtoData.spData.length > 0) ||
  62. (user.surveyCtoData.spSchemeData && user.surveyCtoData.spSchemeData.length > 0)
  63. if (!hasImplementationData) {
  64. return false;
  65. }
  66. }
  67. if (filteredCommunitySet.size > 0) {
  68. const communitiesForUser = user.detailedProfile.communities.map(community => community.name);
  69. const hasCommunities = communitiesForUser.some(community => filteredCommunitySet.has(community));
  70. if (!hasCommunities) {
  71. return false;
  72. }
  73. }
  74. if (filteredCountrySet.size > 0) {
  75. const countriesForUser = user.detailedProfile.branchLocationCountries.map(country => country.name).concat(user.detailedProfile.partnerLocation);
  76. const hasCountries = countriesForUser.some(country => filteredCountrySet.has(country));
  77. if (!hasCountries) {
  78. return false;
  79. }
  80. }
  81. if (filteredStateSet.size > 0) {
  82. const statesForUser = user.detailedProfile.states.map(state => state.name).concat(user.detailedProfile.state);
  83. const hasStates = statesForUser.some(state => filteredStateSet.has(state));
  84. if (!hasStates) {
  85. return false;
  86. }
  87. }
  88. if (filteredDistrictSet.size > 0) {
  89. const districtsForUser = user.detailedProfile.districts.map(district => district.name).concat(user.detailedProfile.district);
  90. const hasDistricts = districtsForUser.some(district => filteredDistrictSet.has(district));
  91. if (!hasDistricts) {
  92. return false;
  93. }
  94. }
  95. if (filteredPartnerTypeSet.size > 0) {
  96. const partnerTypeOfUser = PARTNER_MAPPING.find(mapping => mapping.portalId === user.portalId)?.type;
  97. const hasPartnerType = partnerTypeOfUser && filteredPartnerTypeSet.has(partnerTypeOfUser);
  98. if (!hasPartnerType) {
  99. return false;
  100. }
  101. }
  102. if (this.searchText) {
  103. const hasSearchText = (!!user.organizationBasicInfo.name && user.organizationBasicInfo.name.toLowerCase().trim().includes(searchText));
  104. if (!hasSearchText) {
  105. return false;
  106. }
  107. }
  108. return true;
  109. });
  110. this.partialLoad(this.userData.slice(0, 100))
  111. }, (e) => console.log(e));
  112. this.selectedPartnerList = [];
  113. }
  114. partialLoad(data: Array<UserData>) {
  115. this.tempUserData = data;
  116. }
  117. loadMore() {
  118. this.partialLoad(this.userData.slice(0, this.tempUserData.length + 10));
  119. }
  120. showPartnerDetails(partner: any) {
  121. this.router.navigate(['dashboard/partners/partner-details'], { queryParams: { data: JSON.stringify(partner) } });
  122. }
  123. selectPartner(partner: any) {
  124. if (this.selectedPartnerList) {
  125. if (this.selectedPartnerList.some(x => JSON.stringify(x) === JSON.stringify(partner))) {
  126. this.selectedPartnerList = this.selectedPartnerList.filter(x => JSON.stringify(x) !== JSON.stringify(partner))
  127. } else this.selectedPartnerList.push(partner)
  128. }
  129. }
  130. loadExportData() {
  131. let exportData: Array<any> = [];
  132. let exportHiiData: Array<any> = [];
  133. let exportSpData: Array<any> = [];
  134. let exportSPSchemaData: Array<any> = [];
  135. let surveyCtoData: Array<any> = [];
  136. for (const partner of this.selectedPartnerList) {
  137. let partnerDetails = {
  138. "PortalID": partner.portalId ? partner.portalId : '-',
  139. // Primary Contact
  140. "Primary Name": partner.primaryContact.name,
  141. "Primary Contact Number": partner.primaryContact.contactNumber,
  142. "Primary Email": partner.primaryContact.email,
  143. "Primary Designation": partner.primaryContact.designation,
  144. // Basic Info
  145. "Areas Of Work": partner.organizationBasicInfo.areasOfWork ? partner.organizationBasicInfo.areasOfWork.toString() : '',
  146. "Name": partner.organizationBasicInfo.name,
  147. "Reason For Becoming Member": partner.organizationBasicInfo.reasonForBecomingMember,
  148. "Referral Name": partner.organizationBasicInfo.referralName,
  149. "Source": partner.organizationBasicInfo.source,
  150. "Type": partner.organizationBasicInfo.type,
  151. "Website": partner.organizationBasicInfo.website,
  152. "Would Like Updates": partner.organizationBasicInfo.wouldLikeUpdates,
  153. // Alternative Contact
  154. 'Alternative Name': partner.alternateContact.name,
  155. 'Alternative ContactNumber': partner.alternateContact.contactNumber,
  156. 'Alternative Email': partner.alternateContact.email,
  157. 'Alternative Designation': partner.alternateContact.designation,
  158. // Detailed Profile
  159. 'Bio': partner.detailedProfile.bio,
  160. 'Branch Location Countries': partner.detailedProfile.branchLocationCountries ? partner.detailedProfile.branchLocationCountries.toString() : '',
  161. "Communities": partner.detailedProfile.communities ? partner.detailedProfile.communities.toString() : '',
  162. "District": partner.detailedProfile.district,
  163. "Districts": partner.detailedProfile.districts ? partner.detailedProfile.districts.toString() : '',
  164. "Files": partner.detailedProfile.files,
  165. "Have Branches In Other Districts": partner.detailedProfile.haveBranchesInOtherDistricts,
  166. "Logo": partner.detailedProfile.logo,
  167. "partner Location": partner.detailedProfile.partnerLocation,
  168. "Preferred Languages": partner.detailedProfile.preferredLanguages ? partner.detailedProfile.preferredLanguages.toString() : '',
  169. "Preferred Mode Of Communications": partner.detailedProfile.preferredModeOfCommunications ? partner.detailedProfile.preferredModeOfCommunications.toString() : '',
  170. "State": partner.detailedProfile.state,
  171. "States": partner.detailedProfile.states ? partner.detailedProfile.states.toString() : '',
  172. "Total Reach Of Organization": partner.detailedProfile.totalReachOfOrganization,
  173. "Year Of Establishment": partner.detailedProfile.yearOfEstablishment,
  174. // Strength
  175. "Other Specific Support Required": partner.strengthAndCapability.otherSpecificSupportRequired,
  176. "Primary Areas Of Support Offered": partner.strengthAndCapability.otherSpecificSupportRequired ? partner.strengthAndCapability.otherSpecificSupportRequired.toString() : '',
  177. "Primary Areas Of Support Offered Description": partner.strengthAndCapability.primaryAreasOfSupportOffered,
  178. "Primary Areas Of Support Offered Other": partner.strengthAndCapability.primaryAreasOfSupportOfferedDescription,
  179. "Primary Areas Of Support Required": partner.strengthAndCapability.primaryAreasOfSupportOfferedOther ? partner.strengthAndCapability.primaryAreasOfSupportOfferedOther.toString() : '',
  180. "Primary Areas Of Support Required Description": partner.strengthAndCapability.primaryAreasOfSupportRequired,
  181. "Primary Areas Of Support Required Other": partner.strengthAndCapability.primaryAreasOfSupportRequiredOther,
  182. }
  183. exportData.push(partnerDetails);
  184. let surveyCtoHIIData = [];
  185. for (const hiidata of partner.surveyCtoData.hiiData) {
  186. let partnerHiiDetails = {
  187. "HII Id": hiidata.id,
  188. "HII Name": hiidata.name,
  189. "HII Packages Health": hiidata.packagesHealth,
  190. "HII Implementation Status": hiidata.implementationStatus,
  191. "HII Disaggregation Note": hiidata.disaggregationNote,
  192. "HII No Of Males": hiidata.id,
  193. "HII No Of Females": hiidata.noOfMales,
  194. "HII No Of Transgender": hiidata.noOfTransgender,
  195. "HII Health Remarks": hiidata.healthRemarks,
  196. "HII Relevant Documents": hiidata.relevantDocuments,
  197. }
  198. surveyCtoHIIData.push(partnerHiiDetails)
  199. }
  200. surveyCtoHIIData.forEach(data => {
  201. exportHiiData.push(data)
  202. })
  203. let surveyCtoSpData = []
  204. for (const spData of partner.surveyCtoData.spData) {
  205. let partnerSpDetails = {
  206. "SP Id": spData.id,
  207. "SP Name": spData.name,
  208. "SP Status": spData.status,
  209. 'SP Reason Name': spData.reasonName,
  210. "SP Female Number": spData.femaleNo ? spData.femaleNo : '',
  211. "SP Male Number": spData.maleNo ? spData.maleNo : '',
  212. "Tg Number": spData.tgNo ? spData.tgNo : '',
  213. "SP TotalAggregation": spData.totalAggregation,
  214. "SP OtherRemarks": spData.otherRemarks,
  215. "SP RelevantDocuments": spData.relevantDocuments,
  216. }
  217. surveyCtoSpData.push(partnerSpDetails)
  218. }
  219. surveyCtoSpData.forEach(data => {
  220. exportSpData.push(data)
  221. })
  222. let surveyCtoSpSchemaData = []
  223. for (const spSchema of partner.surveyCtoData.spSchemeData) {
  224. let partnerSpSchemaDetails = {
  225. "SPSchema SchemeId": spSchema.schemeId,
  226. "SPSchema Count": spSchema.count,
  227. }
  228. surveyCtoSpSchemaData.push(partnerSpSchemaDetails)
  229. }
  230. surveyCtoSpSchemaData.forEach(data => {
  231. exportSPSchemaData.push(data)
  232. })
  233. }
  234. surveyCtoData.push(exportHiiData)
  235. surveyCtoData.push(exportSpData)
  236. surveyCtoData.push(exportSPSchemaData)
  237. this.exportData = exportData;
  238. this.exportSurveyCtoData = surveyCtoData
  239. }
  240. isInputChecked(partner: any) {
  241. return this.selectedPartnerList.some(x => JSON.stringify(x) === JSON.stringify(partner));
  242. }
  243. isAllInputChecked() {
  244. return JSON.stringify(this.selectedPartnerList) === JSON.stringify(this.tempUserData);
  245. }
  246. selectAllPartner() {
  247. JSON.stringify(this.selectedPartnerList) === JSON.stringify(this.tempUserData) ? this.selectedPartnerList = [] : this.selectedPartnerList = this.tempUserData;
  248. }
  249. exportProfileData(exportType: exportType) {
  250. this.loadExportData();
  251. const fileTypeCSV = 'text/csv;charset=utf-8;',
  252. element = document.createElement('a');
  253. let blob;
  254. const partnerData = XLSX.utils.json_to_sheet(this.exportData);
  255. const hiiData = XLSX.utils.json_to_sheet(this.exportSurveyCtoData[0]);
  256. const spData = XLSX.utils.json_to_sheet(this.exportSurveyCtoData[1]);
  257. const spSchema = XLSX.utils.json_to_sheet(this.exportSurveyCtoData[2]);
  258. const wb = XLSX.utils.book_new();
  259. if (exportType === 'CSV') {
  260. if (this.isProfileData) {
  261. let csvData = Papa.unparse(this.exportData);
  262. blob = new Blob([csvData], { type: fileTypeCSV });
  263. let url = URL.createObjectURL(blob);
  264. element.href = url;
  265. element.setAttribute('download', 'ProfileData' + '.csv');
  266. element.click();
  267. } else if (this.isImplementationData) {
  268. this.exportJsonToCSV(this.exportSurveyCtoData, ['HiiData', 'SPData', 'SPSchemaData'])
  269. } else {
  270. let completeData = this.exportSurveyCtoData
  271. completeData.push(this.exportData)
  272. console.log(completeData)
  273. this.exportJsonToCSV(completeData, ['HiiData', 'SPData', 'SPSchemaData', 'ProfileData',])
  274. }
  275. } else {
  276. if (this.isProfileData) {
  277. XLSX.utils.book_append_sheet(wb, partnerData, "Partner Profile");
  278. XLSX.writeFile(wb, 'PartnerProfile.xlsx');
  279. } else if (this.isImplementationData) {
  280. XLSX.utils.book_append_sheet(wb, hiiData, "Hii Data");
  281. XLSX.utils.book_append_sheet(wb, spData, "Sp DATA");
  282. XLSX.utils.book_append_sheet(wb, spSchema, "SP Schema Data");
  283. XLSX.writeFile(wb, 'PartnerProfile.xlsx');
  284. } else {
  285. XLSX.utils.book_append_sheet(wb, partnerData, "PartneData");
  286. XLSX.utils.book_append_sheet(wb, hiiData, "HiiData");
  287. XLSX.utils.book_append_sheet(wb, spData, "SPData");
  288. XLSX.utils.book_append_sheet(wb, spSchema, "SPSchemaData");
  289. XLSX.writeFile(wb, 'PartnerProfile.xlsx');
  290. }
  291. }
  292. }
  293. exportJsonToCSV(csvData: any, fileName?: any) {
  294. const fileTypeCSV = 'text/csv;charset=utf-8;',
  295. element = document.createElement('a');
  296. let blob;
  297. csvData.forEach((exportData: any, index: number) => {
  298. console.log(exportData)
  299. let csvData = Papa.unparse(exportData);
  300. blob = new Blob([csvData], { type: fileTypeCSV });
  301. let url = URL.createObjectURL(blob);
  302. element.href = url;
  303. element.setAttribute('download', fileName[index] + '.csv');
  304. element.click();
  305. });
  306. }
  307. }