This repository has been archived on 2026-03-21. You can view files and clone it, but cannot push or open issues or pull requests.
payfrit-biz/api/users/search.cfm
John Mizerek 39448c5d91 Fix prefixed column names in auth, orders, portal team, users search, workers APIs
Updated Users (UserID, UserFirstName, UserLastName, UserEmailAddress, UserContactNumber),
ServicePoints (ServicePointID, ServicePointName, ServicePointTypeID), and Businesses
(BusinessID, BusinessName, BusinessTaxRate, BusinessPhone) column references with proper
prefixed names and AS aliases for API compatibility.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-31 17:43:33 -08:00

93 lines
2.8 KiB
Text

<cfsetting showdebugoutput="false">
<cfsetting enablecfoutputonly="true">
<cfcontent type="application/json; charset=utf-8" reset="true">
<cfscript>
// Search users by phone, email, or username
// Input: Query (search term)
// Output: { OK: true, USERS: [...] }
function apiAbort(required struct payload) {
writeOutput(serializeJSON(payload));
abort;
}
function readJsonBody() {
var raw = getHttpRequestData().content;
if (isNull(raw)) raw = "";
if (!len(trim(raw))) return {};
try {
var data = deserializeJSON(raw);
if (isStruct(data)) return data;
} catch (any e) {}
return {};
}
try {
data = readJsonBody();
query = structKeyExists(data, "Query") ? trim(data.Query) : "";
currentUserId = val(structKeyExists(data, "CurrentUserID") ? data.CurrentUserID : 0);
if (len(query) < 3) {
apiAbort({ "OK": true, "USERS": [], "MESSAGE": "Query must be at least 3 characters" });
}
// Search by phone, email, or first/last name
// Exclude the current user from results
searchTerm = "%" & query & "%";
qUsers = queryExecute("
SELECT
u.UserID AS ID,
u.UserFirstName AS FirstName,
u.UserLastName AS LastName,
u.UserEmailAddress AS EmailAddress,
u.UserContactNumber AS ContactNumber,
u.UserAvatarPath
FROM Users u
WHERE u.UserID != :currentUserId
AND (
u.UserContactNumber LIKE :searchTerm
OR u.UserEmailAddress LIKE :searchTerm
OR u.UserFirstName LIKE :searchTerm
OR u.UserLastName LIKE :searchTerm
OR CONCAT(u.UserFirstName, ' ', u.UserLastName) LIKE :searchTerm
)
ORDER BY u.UserFirstName, u.UserLastName
LIMIT 10
", {
currentUserId: { value: currentUserId, cfsqltype: "cf_sql_integer" },
searchTerm: { value: searchTerm, cfsqltype: "cf_sql_varchar" }
}, { datasource: "payfrit" });
users = [];
for (user in qUsers) {
// Mask phone number for privacy (show last 4 digits only)
maskedPhone = "";
if (len(trim(user.ContactNumber)) >= 4) {
maskedPhone = "***-***-" & right(user.ContactNumber, 4);
}
arrayAppend(users, {
"UserID": user.UserID,
"Name": trim(user.FirstName & " " & user.LastName),
"Email": user.EmailAddress,
"Phone": maskedPhone,
"AvatarUrl": len(trim(user.UserAvatarPath)) ? "https://biz.payfrit.com/uploads/" & user.UserAvatarPath : ""
});
}
apiAbort({
"OK": true,
"USERS": users,
"COUNT": arrayLen(users)
});
} catch (any e) {
apiAbort({
"OK": false,
"ERROR": "server_error",
"MESSAGE": e.message
});
}
</cfscript>