|
| 1 | +import {getPrivateUser} from 'shared/utils' |
| 2 | +import {createSupabaseDirectClient, SupabaseDirectClient} from 'shared/supabase/init' |
| 3 | +import {Notification} from 'common/notifications' |
| 4 | +import {insertNotificationToSupabase} from 'shared/supabase/notifications' |
| 5 | +import {getProfile} from 'shared/profiles/supabase' |
| 6 | +import {tryCatch} from "common/util/try-catch"; |
| 7 | +import {Row} from "common/supabase/utils"; |
| 8 | + |
| 9 | +export const createVoteNotificationAll = async () => { |
| 10 | + const pg = createSupabaseDirectClient() |
| 11 | + const {data: users, error} = await tryCatch( |
| 12 | + pg.many<Row<'users'>>('select * from users') |
| 13 | + ) |
| 14 | + |
| 15 | + if (error) { |
| 16 | + console.error('Error fetching users', error) |
| 17 | + return |
| 18 | + } |
| 19 | + |
| 20 | + if (!users) { |
| 21 | + console.error('No users found') |
| 22 | + return |
| 23 | + } |
| 24 | + |
| 25 | + for (const user of users) { |
| 26 | + try { |
| 27 | + await createVoteNotification(user, pg) |
| 28 | + } catch (e) { |
| 29 | + console.error('Failed to create vote notification', e, user) |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + return { |
| 34 | + success: true, |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +export const createVoteNotification = async (user: Row<'users'>, pg: SupabaseDirectClient) => { |
| 39 | + const targetPrivateUser = await getPrivateUser(user.id) |
| 40 | + const profile = await getProfile(user.id) |
| 41 | + |
| 42 | + if (!targetPrivateUser || !profile) return |
| 43 | + |
| 44 | + const id = `vote-${Date.now()}` |
| 45 | + const notification: Notification = { |
| 46 | + id, |
| 47 | + userId: user.id, |
| 48 | + reason: 'vote', |
| 49 | + createdTime: Date.now(), |
| 50 | + isSeen: false, |
| 51 | + sourceId: '', |
| 52 | + sourceType: 'vote', |
| 53 | + sourceUpdateType: 'created', |
| 54 | + sourceUserName: '', |
| 55 | + sourceUserUsername: 'vote', |
| 56 | + sourceSlug: '/vote', |
| 57 | + sourceUserAvatarUrl: 'https://firebasestorage.googleapis.com/v0/b/compass-130ba.firebasestorage.app/o/misc%2Fvote-icon-design-free-vector.jpg?alt=media&token=f70b6d14-0511-49b2-830d-e7cabf7bb751', |
| 58 | + title: 'New Proposals & Votes Page', |
| 59 | + sourceText: 'Create proposals and vote on other people\'s suggestions!', |
| 60 | + } |
| 61 | + console.log('notification', user.username) |
| 62 | + return await insertNotificationToSupabase(notification, pg) |
| 63 | +} |
0 commit comments