feat(api): create subscription management endpoint
This commit is contained in:
		
							parent
							
								
									8d8ab31bfc
								
							
						
					
					
						commit
						3ce048ced2
					
				
							
								
								
									
										43
									
								
								server/api/user/me/subscriptions.put.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										43
									
								
								server/api/user/me/subscriptions.put.ts
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,43 @@ | |||||||
|  | import { SubPayload } from "~/lib/client/types/form/favSub"; | ||||||
|  | import isLoggedIn from "~/lib/server/middlewareButNotReally/isLoggedIn"; | ||||||
|  | import { User } from "~/models/user"; | ||||||
|  | 
 | ||||||
|  | export default eventHandler(async (ev) => { | ||||||
|  | 	isLoggedIn(ev); | ||||||
|  | 	// note: debounced batched update
 | ||||||
|  | 	const body = await readBody<SubPayload>(ev); | ||||||
|  | 	await User.findByIdAndUpdate(ev.context.currentUser!._id, { | ||||||
|  | 		$pull: { | ||||||
|  | 			"subscriptions.authors": { | ||||||
|  | 				$in: body.pull.authors, | ||||||
|  | 			}, | ||||||
|  | 			"subscriptions.bands": { | ||||||
|  | 				$in: body.pull.bands, | ||||||
|  | 			}, | ||||||
|  | 			"subscriptions.stories": { | ||||||
|  | 				$in: body.pull.stories, | ||||||
|  | 			}, | ||||||
|  | 		}, | ||||||
|  | 	}); | ||||||
|  | 	const nu = await User.findByIdAndUpdate( | ||||||
|  | 		ev.context.currentUser!._id, | ||||||
|  | 		{ | ||||||
|  | 			$addToSet: { | ||||||
|  | 				"subscriptions.authors": { | ||||||
|  | 					$each: body.pull.authors, | ||||||
|  | 				}, | ||||||
|  | 				"subscriptions.bands": { | ||||||
|  | 					$each: body.pull.bands, | ||||||
|  | 				}, | ||||||
|  | 				"subscriptions.stories": { | ||||||
|  | 					$each: body.pull.stories, | ||||||
|  | 				}, | ||||||
|  | 			}, | ||||||
|  | 		}, | ||||||
|  | 		{ new: true }, | ||||||
|  | 	); | ||||||
|  | 	return { | ||||||
|  | 		success: true, | ||||||
|  | 		data: nu?.subscriptions, | ||||||
|  | 	}; | ||||||
|  | }); | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user