| 
									
										
										
										
											2021-08-21 00:46:43 -08:00
										 |  |  | import { useAsync, ref, reactive } from "@nuxtjs/composition-api"; | 
					
						
							| 
									
										
										
										
											2021-08-07 15:12:25 -08:00
										 |  |  | import { toastLoading, loader } from "./use-toast"; | 
					
						
							| 
									
										
										
										
											2022-05-21 21:22:02 +02:00
										 |  |  | import { AllBackups, BackupOptions } from "~/types/api-types/admin"; | 
					
						
							| 
									
										
										
										
											2021-11-06 11:28:47 -08:00
										 |  |  | import { useUserApi } from "~/composables/api"; | 
					
						
							| 
									
										
										
										
											2021-08-07 15:12:25 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-21 21:22:02 +02:00
										 |  |  | interface ImportBackup { | 
					
						
							|  |  |  |   name: string; | 
					
						
							|  |  |  |   options: BackupOptions; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-07 15:12:25 -08:00
										 |  |  | const backups = ref<AllBackups>({ | 
					
						
							|  |  |  |   imports: [], | 
					
						
							|  |  |  |   templates: [], | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function setBackups(newBackups: AllBackups | null) { | 
					
						
							|  |  |  |   if (newBackups) { | 
					
						
							| 
									
										
										
										
											2022-01-16 03:38:11 +01:00
										 |  |  |     backups.value = newBackups; | 
					
						
							| 
									
										
										
										
											2021-08-07 15:12:25 -08:00
										 |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-21 00:46:43 -08:00
										 |  |  | function optionsFactory() { | 
					
						
							|  |  |  |   return { | 
					
						
							|  |  |  |     tag: "", | 
					
						
							|  |  |  |     templates: [], | 
					
						
							|  |  |  |     options: { | 
					
						
							|  |  |  |       recipes: true, | 
					
						
							|  |  |  |       settings: true, | 
					
						
							|  |  |  |       themes: true, | 
					
						
							|  |  |  |       pages: true, | 
					
						
							|  |  |  |       users: true, | 
					
						
							|  |  |  |       groups: true, | 
					
						
							|  |  |  |       notifications: true, | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-07 15:12:25 -08:00
										 |  |  | export const useBackups = function (fetch = true) { | 
					
						
							| 
									
										
										
										
											2021-11-06 11:28:47 -08:00
										 |  |  |   const api = useUserApi(); | 
					
						
							| 
									
										
										
										
											2021-08-07 15:12:25 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-21 00:46:43 -08:00
										 |  |  |   const backupOptions = reactive(optionsFactory()); | 
					
						
							|  |  |  |   const deleteTarget = ref(""); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const selected = ref<ImportBackup | null>({ | 
					
						
							|  |  |  |     name: "", | 
					
						
							|  |  |  |     options: { | 
					
						
							|  |  |  |       recipes: true, | 
					
						
							|  |  |  |       settings: true, | 
					
						
							|  |  |  |       themes: true, | 
					
						
							|  |  |  |       groups: true, | 
					
						
							|  |  |  |       users: true, | 
					
						
							|  |  |  |       notifications: true, | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-07 15:12:25 -08:00
										 |  |  |   function getBackups() { | 
					
						
							|  |  |  |     const backups = useAsync(async () => { | 
					
						
							|  |  |  |       const { data } = await api.backups.getAll(); | 
					
						
							|  |  |  |       return data; | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |     return backups; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   async function refreshBackups() { | 
					
						
							|  |  |  |     const { data } = await api.backups.getAll(); | 
					
						
							|  |  |  |     if (data) { | 
					
						
							|  |  |  |       setBackups(data); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-21 00:46:43 -08:00
										 |  |  |   async function createBackup() { | 
					
						
							| 
									
										
										
										
											2021-08-07 15:12:25 -08:00
										 |  |  |     loader.info("Creating Backup..."); | 
					
						
							| 
									
										
										
										
											2021-08-21 00:46:43 -08:00
										 |  |  |     const { response } = await api.backups.createOne(backupOptions); | 
					
						
							| 
									
										
										
										
											2021-08-07 15:12:25 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (response && response.status === 201) { | 
					
						
							|  |  |  |       refreshBackups(); | 
					
						
							|  |  |  |       toastLoading.open = false; | 
					
						
							| 
									
										
										
										
											2021-08-21 00:46:43 -08:00
										 |  |  |       Object.assign(backupOptions, optionsFactory()); | 
					
						
							| 
									
										
										
										
											2021-08-07 15:12:25 -08:00
										 |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-21 00:46:43 -08:00
										 |  |  |   async function deleteBackup() { | 
					
						
							|  |  |  |     const { response } = await api.backups.deleteOne(deleteTarget.value); | 
					
						
							| 
									
										
										
										
											2021-08-07 15:12:25 -08:00
										 |  |  |     if (response && response.status === 200) { | 
					
						
							|  |  |  |       refreshBackups(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-21 00:46:43 -08:00
										 |  |  |   async function importBackup() { | 
					
						
							| 
									
										
										
										
											2021-08-07 15:12:25 -08:00
										 |  |  |     loader.info("Import Backup..."); | 
					
						
							| 
									
										
										
										
											2021-08-21 00:46:43 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (!selected.value) { | 
					
						
							|  |  |  |       return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const { response } = await api.backups.restoreDatabase(selected.value.name, selected.value.options); | 
					
						
							| 
									
										
										
										
											2021-08-07 15:12:25 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (response && response.status === 200) { | 
					
						
							|  |  |  |       refreshBackups(); | 
					
						
							|  |  |  |       loader.close(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (fetch) { | 
					
						
							|  |  |  |     refreshBackups(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-21 00:46:43 -08:00
										 |  |  |   return { | 
					
						
							|  |  |  |     getBackups, | 
					
						
							|  |  |  |     refreshBackups, | 
					
						
							|  |  |  |     deleteBackup, | 
					
						
							|  |  |  |     importBackup, | 
					
						
							|  |  |  |     createBackup, | 
					
						
							|  |  |  |     backups, | 
					
						
							|  |  |  |     backupOptions, | 
					
						
							|  |  |  |     deleteTarget, | 
					
						
							|  |  |  |     selected, | 
					
						
							|  |  |  |   }; | 
					
						
							| 
									
										
										
										
											2021-08-07 15:12:25 -08:00
										 |  |  | }; |