import Notification from '@entities/Notification' ; import User from '@entities/User' ; import { NotificationType } from '@enums/Notifications' ; export const generatedNumber = ( n = 10 ) => { const multiplier = 10 ** ( n - 1 ) ; return Math . floor ( 1 * multiplier + Math . random ( ) * 9 * multiplier ) ; } ; export const searchJSON = ( json , key , value ) => { return json . findIndex ( ( data ) => data [ key ] === value ) !== - 1 ; } ; export const getShortMonth = ( ) => { const shortMonths = [ 'Jan' , 'Feb' , 'Mar' , 'Apr' , 'May' , 'Jun' , 'Jul' , 'Aug' , 'Sep' , 'Oct' , 'Nov' , 'Dec' , ] ; const today = new Date ( ) ; const year = today . getFullYear ( ) ; const day = today . getDate ( ) ; return shortMonths [ today . getMonth ( ) ] + ` ${ day }, ${ year }` ; } ; export const createNotification = async ( { userId , performerId , type } ) => { const oneTimeNotifications = [ NotificationType . Follow ] ; const user = await User . findOne ( userId ) ; const performer = await User . findOne ( performerId ) ; const prevNotifs = ( await Notification . find ( { where : { user , performer , type , } , } ) ) . length ; if ( prevNotifs >= 1 && oneTimeNotifications . includes ( type ) ) { return ; } const notification = Notification . create ( ) ; notification . user = Promise . resolve ( user ) ; notification . performer = Promise . resolve ( performer ) ; notification . type = type ; notification . read = false ; notification . createdAt = String ( Date . now ( ) ) ; notification . save ( ) ; } ;