just another day

This commit is contained in:
2024-10-25 16:57:25 -05:00
parent 0e46c630a6
commit e84597883b
11 changed files with 882 additions and 1401 deletions

View File

@ -1,14 +1,18 @@
import * as FileSystem from 'expo-file-system';
import type { User, RelationshipData, Message } from '@/constants/Types';
import type { User, RelationshipData, Message, Countdown } from '@/constants/Types';
const API_KEY = process.env.EXPO_PUBLIC_API_KEY ?? '';
const API_URL = process.env.EXPO_PUBLIC_API_URL ?? '';
export const getInitialDataByAppleId = async (appleId: string) => {
try {
const apiUrl = `${process.env.EXPO_PUBLIC_API_URL}/api/users/getInitialDataByAppleId`;
const apiUrl = `${API_URL}/api/users/getInitialDataByAppleId`;
const response = await fetch((apiUrl + `?appleId=${appleId}`), {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'x-api-key': process.env.EXPO_PUBLIC_API_KEY ?? '',
'x-api-key': API_KEY,
},
});
return response;
@ -21,12 +25,12 @@ export const getInitialDataByAppleId = async (appleId: string) => {
export const createUser =
async (appleId: string, email: string, fullName: string, pushToken: string) => {
try {
const apiUrl = `${process.env.EXPO_PUBLIC_API_URL}/api/users/createUser`;
const apiUrl = `${API_URL}/api/users/createUser`;
const response = await fetch(apiUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'x-api-key': process.env.EXPO_PUBLIC_API_KEY ?? '',
'x-api-key': API_KEY,
},
body: JSON.stringify({
appleId: appleId,
@ -50,12 +54,12 @@ export const createUser =
export const updatePushToken = async (userId: number, pushToken: string) => {
try {
const apiUrl = `${process.env.EXPO_PUBLIC_API_URL}/api/users/updatePushToken`;
const apiUrl = `${API_URL}/api/users/updatePushToken`;
const response = await fetch(apiUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'x-api-key': process.env.EXPO_PUBLIC_API_KEY ?? '',
'x-api-key': API_KEY,
},
body: JSON.stringify({
userId: userId,
@ -76,14 +80,14 @@ export const updatePushToken = async (userId: number, pushToken: string) => {
export const updateProfilePicture = async (userId: number, pfpUrl: string) => {
try {
const apiUrl = `${process.env.EXPO_PUBLIC_API_URL}/api/users/updatePfp`;
const apiUrl = `${API_URL}/api/users/updatePfp`;
const response = await FileSystem.uploadAsync(apiUrl, pfpUrl, {
fieldName: 'file',
httpMethod: 'POST',
uploadType: FileSystem.FileSystemUploadType.MULTIPART,
parameters: { userId: userId.toString() },
headers: {
'x-api-key': process.env.EXPO_PUBLIC_API_KEY ?? '',
'x-api-key': API_KEY,
},
});
if (response.status !== 200)
@ -100,10 +104,10 @@ export const updateProfilePicture = async (userId: number, pfpUrl: string) => {
export const checkRelationshipStatus = async (userId: number) => {
try {
const apiUrl = `${process.env.EXPO_PUBLIC_API_URL}/api/relationships/checkStatus`;
const apiUrl = `${API_URL}/api/relationships/checkStatus`;
const response = await fetch((apiUrl + `?userId=${userId}`), {
headers: {
'x-api-key': process.env.EXPO_PUBLIC_API_KEY ?? '',
'x-api-key': API_KEY,
},
});
if (!response.ok) {
@ -121,12 +125,12 @@ export const checkRelationshipStatus = async (userId: number) => {
export const updateRelationshipStatus = async (userId: number, status: 'accepted' | 'rejected') => {
try {
const apiUrl = `${process.env.EXPO_PUBLIC_API_URL}/api/relationships/updateStatus`;
const apiUrl = `${API_URL}/api/relationships/updateStatus`;
const response = await fetch(apiUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'x-api-key': process.env.EXPO_PUBLIC_API_KEY ?? '',
'x-api-key': API_KEY,
},
body: JSON.stringify({
userId: userId,
@ -151,10 +155,10 @@ export const updateRelationshipStatus = async (userId: number, status: 'accepted
export const searchUsers = async (userId: number, searchTerm: string) => {
try {
const apiUrl = `${process.env.EXPO_PUBLIC_API_URL}/api/users/search`;
const apiUrl = `${API_URL}/api/users/search`;
const response = await fetch((apiUrl + `?userId=${userId}&searchTerm=${searchTerm}`), {
headers: {
'x-api-key': process.env.EXPO_PUBLIC_API_KEY ?? '',
'x-api-key': API_KEY,
},
});
if (!response.ok) {
@ -173,12 +177,12 @@ export const searchUsers = async (userId: number, searchTerm: string) => {
export const sendRelationshipRequest = async (userId: number, targetUserId: number) => {
if (!userId || !targetUserId || isNaN(userId) || isNaN(targetUserId)) return;
try {
const apiUrl = `${process.env.EXPO_PUBLIC_API_URL}/api/relationships/createRequest`;
const apiUrl = `${API_URL}/api/relationships/createRequest`;
const response = await fetch(apiUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'x-api-key': process.env.EXPO_PUBLIC_API_KEY ?? '',
'x-api-key': API_KEY,
},
body: JSON.stringify({ userId: userId, targetUserId: targetUserId }),
});
@ -198,10 +202,10 @@ export const sendRelationshipRequest = async (userId: number, targetUserId: numb
export const getMessages = async (userId: number, limit: number = 20, offset: number = 0) => {
if (!userId || isNaN(userId)) return;
try {
const apiUrl = `${process.env.EXPO_PUBLIC_API_URL}/api/messages/get`;
const apiUrl = `${API_URL}/api/messages/get`;
const response = await fetch((apiUrl+`?userId=${userId}&limit=${limit}&offset=${offset}`), {
headers: {
'x-api-key': process.env.EXPO_PUBLIC_API_KEY ?? '',
'x-api-key': API_KEY,
},
});
if (!response.ok) {
@ -219,12 +223,12 @@ export const getMessages = async (userId: number, limit: number = 20, offset: nu
export const sendMessage = async (message: Message) => {
try {
const apiUrl = `${process.env.EXPO_PUBLIC_API_URL}/api/messages/send`;
const apiUrl = `${API_URL}/api/messages/send`;
const response = await fetch(apiUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'x-api-key': process.env.EXPO_PUBLIC_API_KEY ?? '',
'x-api-key': API_KEY,
},
body: JSON.stringify({
message: message,
@ -242,3 +246,51 @@ export const sendMessage = async (message: Message) => {
throw error;
}
};
export const setCountdown = async (userId: number, countdown: Countdown ) => {
try {
const apiUrl = `${API_URL}/api/relationships/countdown/set`;
const response = await fetch(apiUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'x-api-key': API_KEY,
},
body: JSON.stringify({
userId: userId,
countdown: countdown,
}),
});
if (!response.ok) {
throw new Error(
`Error setting countdown: ${response.status} ${response.statusText}`
);
}
const countdownData = await response.json() as Countdown;
return countdownData;
} catch (error: unknown) {
console.error('Error setting countdown:', error);
throw error;
}
};
export const getCountdown = async (userId: number) => {
try {
const apiUrl = `${API_URL}/api/relationships/countdown/get`;
const response = await fetch((apiUrl + `?userId=${userId}`), {
headers: {
'x-api-key': API_KEY,
},
});
if (!response.ok) {
throw new Error(
`Error getting countdown: ${response.status} ${response.statusText}`
);
}
const countdownData = await response.json() as Countdown;
return countdownData;
} catch (error: unknown) {
console.error('Error getting countdown:', error);
throw error;
}
};