mirror of
				https://github.com/strapi/strapi.git
				synced 2025-10-31 01:47:13 +00:00 
			
		
		
		
	
		
			
	
	
		
			42 lines
		
	
	
		
			1.0 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
		
		
			
		
	
	
			42 lines
		
	
	
		
			1.0 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
|   | 'use strict'; | ||
|  | 
 | ||
|  | const { getService } = require('../utils'); | ||
|  | const { | ||
|  |   validateGenerateUIDInput, | ||
|  |   validateCheckUIDAvailabilityInput, | ||
|  |   validateUIDField, | ||
|  | } = require('./validation'); | ||
|  | 
 | ||
|  | module.exports = { | ||
|  |   async generateUID(ctx) { | ||
|  |     const { contentTypeUID, field, data } = await validateGenerateUIDInput(ctx.request.body); | ||
|  | 
 | ||
|  |     await validateUIDField(contentTypeUID, field); | ||
|  | 
 | ||
|  |     const uidService = getService('uid'); | ||
|  | 
 | ||
|  |     ctx.body = { | ||
|  |       data: await uidService.generateUIDField({ contentTypeUID, field, data }), | ||
|  |     }; | ||
|  |   }, | ||
|  | 
 | ||
|  |   async checkUIDAvailability(ctx) { | ||
|  |     const { contentTypeUID, field, value } = await validateCheckUIDAvailabilityInput( | ||
|  |       ctx.request.body | ||
|  |     ); | ||
|  | 
 | ||
|  |     await validateUIDField(contentTypeUID, field); | ||
|  | 
 | ||
|  |     const uidService = getService('uid'); | ||
|  | 
 | ||
|  |     const isAvailable = await uidService.checkUIDAvailability({ contentTypeUID, field, value }); | ||
|  | 
 | ||
|  |     ctx.body = { | ||
|  |       isAvailable, | ||
|  |       suggestion: !isAvailable | ||
|  |         ? await uidService.findUniqueUID({ contentTypeUID, field, value }) | ||
|  |         : null, | ||
|  |     }; | ||
|  |   }, | ||
|  | }; |