remove unused code
This commit is contained in:
parent
6fb7d61ead
commit
a5cf8f9868
|
@ -1,58 +0,0 @@
|
||||||
import { Client, Environment } from "square";
|
|
||||||
import { StatusCodes } from "http-status-codes";
|
|
||||||
|
|
||||||
const client = new Client({
|
|
||||||
environment: Environment.Sandbox,
|
|
||||||
accessToken: process.env.SQUARE_ACCESS_TOKEN,
|
|
||||||
});
|
|
||||||
|
|
||||||
const api = {
|
|
||||||
GET: async (req, res) => {
|
|
||||||
const user = "R7R0NY1Z8WT11D43564EEFKTYR"; // req.headers["x-user"];
|
|
||||||
|
|
||||||
try {
|
|
||||||
const { result: customerResult } = await client.customersApi.retrieveCustomer(user);
|
|
||||||
const { customer } = customerResult;
|
|
||||||
|
|
||||||
res.json(customer.cards);
|
|
||||||
} catch (error) {
|
|
||||||
res.json([]);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
// POST: async (req, res) => {
|
|
||||||
// const user = req.headers["x-user"];
|
|
||||||
// const card = {
|
|
||||||
// cardNonce: "YOUR_CARD_NONCE",
|
|
||||||
// cardholderName: "Amelia Earhart",
|
|
||||||
// billingAddress: {},
|
|
||||||
// verificationToken: "verification_token0",
|
|
||||||
// };
|
|
||||||
|
|
||||||
// card.bodyBillingAddress.addressLine1 = "500 Electric Ave";
|
|
||||||
// card.bodyBillingAddress.addressLine2 = "Suite 600";
|
|
||||||
// card.bodyBillingAddress.addressLine3 = "address_line_38";
|
|
||||||
// card.bodyBillingAddress.locality = "New York";
|
|
||||||
// card.bodyBillingAddress.sublocality = "sublocality2";
|
|
||||||
// card.bodyBillingAddress.administrativeDistrictLevel1 = "NY";
|
|
||||||
// card.bodyBillingAddress.postalCode = "10003";
|
|
||||||
// card.bodyBillingAddress.country = "US";
|
|
||||||
|
|
||||||
// try {
|
|
||||||
// const { result } = await client.customersApi.createCustomerCard(user, card);
|
|
||||||
|
|
||||||
// res.status(StatusCodes.NO_CONTENT);
|
|
||||||
// } catch (error) {
|
|
||||||
// console.log(Object.keys(error));
|
|
||||||
|
|
||||||
// res.status(StatusCodes.BAD_REQUEST);
|
|
||||||
// }
|
|
||||||
// },
|
|
||||||
};
|
|
||||||
|
|
||||||
export default (req, res) => {
|
|
||||||
if (req.method in api) {
|
|
||||||
api[req.method](req, res);
|
|
||||||
} else {
|
|
||||||
res.status(StatusCodes.NOT_FOUND);
|
|
||||||
}
|
|
||||||
};
|
|
|
@ -1,45 +0,0 @@
|
||||||
import { Client, Environment } from "square";
|
|
||||||
import { StatusCodes } from "http-status-codes";
|
|
||||||
|
|
||||||
const client = new Client({
|
|
||||||
environment: Environment.Sandbox,
|
|
||||||
accessToken: process.env.SQUARE_ACCESS_TOKEN,
|
|
||||||
});
|
|
||||||
|
|
||||||
const api = {
|
|
||||||
GET: async (req, res) => {
|
|
||||||
const user = "NBE7TRXZPGZXNBD64JB6DR5AGR"; // req.headers["x-user"];
|
|
||||||
|
|
||||||
try {
|
|
||||||
// get locations for invoices search query
|
|
||||||
const { result: locationsResponse } = await client.locationsApi.listLocations();
|
|
||||||
const { locations } = locationsResponse;
|
|
||||||
|
|
||||||
// create invoices serach query
|
|
||||||
const locationIds = locations.map(({ id }) => id);
|
|
||||||
const customerIds = [user];
|
|
||||||
const filter = { locationIds, customerIds };
|
|
||||||
const sort = { field: "INVOICE_SORT_DATE", order: "DESC" };
|
|
||||||
const query = { filter, sort };
|
|
||||||
|
|
||||||
// query invoices with given search criteria
|
|
||||||
const { result: invoicesResponse } = await client.invoicesApi.searchInvoices({ query, limit: 10 });
|
|
||||||
const { invoices } = invoicesResponse;
|
|
||||||
|
|
||||||
res.json(invoices);
|
|
||||||
} catch (error) {
|
|
||||||
console.log(error);
|
|
||||||
console.log(error?.errors);
|
|
||||||
|
|
||||||
res.json([]); // todo: error handling
|
|
||||||
}
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
export default (req, res) => {
|
|
||||||
if (req.method in api) {
|
|
||||||
return api[req.method](req, res);
|
|
||||||
}
|
|
||||||
|
|
||||||
return res.status(StatusCodes.NOT_FOUND);
|
|
||||||
};
|
|
|
@ -1,46 +0,0 @@
|
||||||
import { Client, Environment } from "square";
|
|
||||||
import { StatusCodes } from "http-status-codes";
|
|
||||||
|
|
||||||
const client = new Client({
|
|
||||||
environment: Environment.Sandbox,
|
|
||||||
accessToken: process.env.SQUARE_ACCESS_TOKEN,
|
|
||||||
});
|
|
||||||
|
|
||||||
const api = {
|
|
||||||
GET: async (req, res) => {
|
|
||||||
try {
|
|
||||||
const user = "NBE7TRXZPGZXNBD64JB6DR5AGR"; // req.headers["x-user"];
|
|
||||||
|
|
||||||
// create subscriptions search query
|
|
||||||
const query = { filter: { customerIds: [user] } };
|
|
||||||
|
|
||||||
// query subscriptions with given search criteria
|
|
||||||
const { result: subscriptionsResponse } = await client.subscriptionsApi.searchSubscriptions({ query });
|
|
||||||
const { subscriptions } = subscriptionsResponse;
|
|
||||||
|
|
||||||
// get active subscription
|
|
||||||
const subscription = subscriptions.find(({ status }) => status === "ACTIVE");
|
|
||||||
|
|
||||||
if (!subscription) {
|
|
||||||
return res.status(StatusCodes.NO_CONTENT).end(); // no active subscription found
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log("....", subscription);
|
|
||||||
|
|
||||||
return res.json(subscription);
|
|
||||||
} catch (error) {
|
|
||||||
console.log(error);
|
|
||||||
console.log(error?.errors);
|
|
||||||
|
|
||||||
return res.status(StatusCodes.BAD_REQUEST).end(); // todo: error handling
|
|
||||||
}
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
export default (req, res) => {
|
|
||||||
if (req.method in api) {
|
|
||||||
return api[req.method](req, res);
|
|
||||||
}
|
|
||||||
|
|
||||||
return res.status(StatusCodes.NOT_FOUND).end();
|
|
||||||
};
|
|
|
@ -1,55 +0,0 @@
|
||||||
import { Client, Environment } from "square";
|
|
||||||
import { StatusCodes } from "http-status-codes";
|
|
||||||
|
|
||||||
const client = new Client({
|
|
||||||
environment: Environment.Sandbox,
|
|
||||||
accessToken: process.env.SQUARE_ACCESS_TOKEN,
|
|
||||||
});
|
|
||||||
|
|
||||||
const cancelSubscription = async (id) => {
|
|
||||||
const { result: subscriptionsResponse } = await client.subscriptionsApi.cancelSubscription(id);
|
|
||||||
const { subscription } = subscriptionsResponse;
|
|
||||||
|
|
||||||
return subscription;
|
|
||||||
};
|
|
||||||
|
|
||||||
const getActiveSubscription = async (customerId) => {
|
|
||||||
// create subscriptions search query
|
|
||||||
const query = { filter: { customerIds: [customerId] } };
|
|
||||||
|
|
||||||
// query subscriptions with given search criteria
|
|
||||||
const { result: subscriptionsResponse } = await client.subscriptionsApi.searchSubscriptions({ query });
|
|
||||||
const { subscriptions } = subscriptionsResponse;
|
|
||||||
|
|
||||||
// get active subscription with a set cancellation date
|
|
||||||
return subscriptions.find(({ status, canceledDate }) => status === "ACTIVE" && !canceledDate);
|
|
||||||
};
|
|
||||||
|
|
||||||
const api = {
|
|
||||||
POST: async (req, res) => {
|
|
||||||
try {
|
|
||||||
const user = "NBE7TRXZPGZXNBD64JB6DR5AGR"; // req.headers["x-user"];
|
|
||||||
const subscription = await getActiveSubscription(user);
|
|
||||||
|
|
||||||
if (!subscription) {
|
|
||||||
return res.status(StatusCodes.BAD_REQUEST).end(); // no active subscription found
|
|
||||||
}
|
|
||||||
|
|
||||||
const canceledSubscription = await cancelSubscription(subscription.id);
|
|
||||||
|
|
||||||
return res.json(canceledSubscription);
|
|
||||||
} catch (error) {
|
|
||||||
console.log(error.errors);
|
|
||||||
|
|
||||||
return res.status(StatusCodes.BAD_REQUEST).end(); // todo: error handling
|
|
||||||
}
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
export default (req, res) => {
|
|
||||||
if (req.method in api) {
|
|
||||||
return api[req.method](req, res);
|
|
||||||
}
|
|
||||||
|
|
||||||
return res.status(StatusCodes.NOT_FOUND).end();
|
|
||||||
};
|
|
|
@ -1,58 +0,0 @@
|
||||||
import { Client, Environment } from "square";
|
|
||||||
import { StatusCodes } from "http-status-codes";
|
|
||||||
|
|
||||||
const client = new Client({
|
|
||||||
environment: Environment.Sandbox,
|
|
||||||
accessToken: process.env.SQUARE_ACCESS_TOKEN,
|
|
||||||
});
|
|
||||||
|
|
||||||
const updateSubscription = async (id, body) => {
|
|
||||||
const { result: subscriptionsResponse } = await client.subscriptionsApi.updateSubscription(id, body);
|
|
||||||
const { subscription } = subscriptionsResponse;
|
|
||||||
|
|
||||||
return subscription;
|
|
||||||
};
|
|
||||||
|
|
||||||
const getActiveCanceledSubscription = async (customerId) => {
|
|
||||||
// create subscriptions search query
|
|
||||||
const query = { filter: { customerIds: [customerId] } };
|
|
||||||
|
|
||||||
// query subscriptions with given search criteria
|
|
||||||
const { result: subscriptionsResponse } = await client.subscriptionsApi.searchSubscriptions({ query });
|
|
||||||
const { subscriptions } = subscriptionsResponse;
|
|
||||||
|
|
||||||
// get active subscription with a set cancellation date
|
|
||||||
return subscriptions.find(({ status, canceledDate }) => status === "ACTIVE" && canceledDate);
|
|
||||||
};
|
|
||||||
|
|
||||||
const api = {
|
|
||||||
POST: async (req, res) => {
|
|
||||||
try {
|
|
||||||
const user = "NBE7TRXZPGZXNBD64JB6DR5AGR"; // req.headers["x-user"];
|
|
||||||
const subscription = await getActiveCanceledSubscription(user);
|
|
||||||
|
|
||||||
if (!subscription) {
|
|
||||||
return res.status(StatusCodes.BAD_REQUEST).end(); // no active subscription with cancel date found
|
|
||||||
}
|
|
||||||
|
|
||||||
// update the subscription setting empty canceledDate
|
|
||||||
const updatedSubscription = await updateSubscription(subscription.id, {
|
|
||||||
subscription: { ...subscription, canceledDate: "" },
|
|
||||||
});
|
|
||||||
|
|
||||||
return res.json(updatedSubscription);
|
|
||||||
} catch (error) {
|
|
||||||
console.log(error.errors);
|
|
||||||
|
|
||||||
return res.status(StatusCodes.BAD_REQUEST).end(); // todo: error handling
|
|
||||||
}
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
export default (req, res) => {
|
|
||||||
if (req.method in api) {
|
|
||||||
return api[req.method](req, res);
|
|
||||||
}
|
|
||||||
|
|
||||||
return res.status(StatusCodes.NOT_FOUND).end();
|
|
||||||
};
|
|
|
@ -53,7 +53,6 @@ export default async (req, res) => {
|
||||||
|
|
||||||
res.json({ sessionId: session.id });
|
res.json({ sessionId: session.id });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log(error);
|
|
||||||
res.status(StatusCodes.BAD_REQUEST).json({ error: { message: error.message } });
|
res.status(StatusCodes.BAD_REQUEST).json({ error: { message: error.message } });
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -32,8 +32,6 @@ export async function getServerSideProps(context) {
|
||||||
|
|
||||||
throw new Error(`Failed to retrieve flow ${flow} with code ${status}`);
|
throw new Error(`Failed to retrieve flow ${flow} with code ${status}`);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log(config.kratos.public, error.message);
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
redirect: {
|
redirect: {
|
||||||
permanent: false,
|
permanent: false,
|
||||||
|
|
|
@ -30,14 +30,10 @@ export const getServerSideProps = authServerSideProps(async (context) => {
|
||||||
headers: { cookie: context.req.headers.cookie },
|
headers: { cookie: context.req.headers.cookie },
|
||||||
});
|
});
|
||||||
|
|
||||||
console.log(flow, status, data);
|
|
||||||
|
|
||||||
if (status === 200) return { props: { flow: data } };
|
if (status === 200) return { props: { flow: data } };
|
||||||
|
|
||||||
throw new Error(`Failed to retrieve flow ${flow} with code ${status}`);
|
throw new Error(`Failed to retrieve flow ${flow} with code ${status}`);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log(error);
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
redirect: {
|
redirect: {
|
||||||
permanent: false,
|
permanent: false,
|
||||||
|
|
Reference in New Issue