Skip to content

Commit

Permalink
Update: 식단,운동 알림 함수
Browse files Browse the repository at this point in the history
  • Loading branch information
butterbeetle committed Aug 13, 2024
1 parent fe73fe2 commit ee0b235
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
6 changes: 5 additions & 1 deletion supabase/functions/sendTodayDietReminder/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ const supabaseKey = Deno.env.get('SUPABASE_SERVICE_ROLE_KEY') as string;

Deno.serve(async () => {
try {
const today = new Date(Date.now() + 9 * 60 * 60 * 1000);
// today.setDate(today.getDate() + 1);
const todayStr = today.toISOString().split('T')[0];
console.log('TODAY___', todayStr);
const supabase = createClient(supabaseUrl, supabaseKey);

const { data: users } = await supabase.from('users').select('id');
Expand All @@ -16,7 +20,7 @@ Deno.serve(async () => {
}

const dietsPromises = users.map((user: any) =>
supabase.from('diets').select('*').eq('userId', user.id).gte('date', new Date().toISOString()),
supabase.from('diets').select('*').eq('userId', user.id).gte('date', todayStr),
);
const results = await Promise.all(dietsPromises);

Expand Down
13 changes: 8 additions & 5 deletions supabase/functions/sendTodayExerciseReminder/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ const supabaseKey = Deno.env.get('SUPABASE_SERVICE_ROLE_KEY') as string;

Deno.serve(async () => {
try {
const today = new Date(Date.now() + 9 * 60 * 60 * 1000);
// today.setDate(today.getDate() + 1);
const todayStr = today.toISOString().split('T')[0];
const supabase = createClient(supabaseUrl, supabaseKey);

const { data: users } = await supabase.from('users').select('id');
Expand All @@ -16,13 +19,13 @@ Deno.serve(async () => {
}

const exercisesPromises = users.map((user: any) =>
supabase.from('exercises').select('*').eq('userId', user.id).gte('date', new Date().toISOString()),
supabase.from('exercises').select('*').eq('userId', user.id).gte('date', todayStr),
);
const results = await Promise.all(exercisesPromises);

console.log('DietsPromises Results___', results);
console.log('ExercisesPromises Results___', results);

const insertDietNotifications = users
const insertExerciseNotifications = users
.filter((_: any, idx: number) => !results[idx]?.data?.length)
.map((user: any) =>
supabase.from('notifications').insert({
Expand All @@ -35,8 +38,8 @@ Deno.serve(async () => {
}),
);

const insertDietNotificationsResults = await Promise.all(insertDietNotifications);
console.log('InsertDietNotifications Results___', insertDietNotificationsResults);
const insertExerciseNotificationsResults = await Promise.all(insertExerciseNotifications);
console.log('Insert Exercise Notifications Results___', insertExerciseNotificationsResults);

return new Response(JSON.stringify({ message: 'Challenges updated successfully', results }), {
headers: { 'Content-Type': 'application/json' },
Expand Down

0 comments on commit ee0b235

Please sign in to comment.